1

我正在尝试以编程方式查找和突出显示 word 文件中的文本,但是当行bool highlighted = range.Find.HitHighlight执行时,会发生异常并且异常消息是Bad variable type. (Exception from HRESULT: 0x80020008 (DISP_E_BADVARTYPE))

我无法找出导致此错误消息的原因。这是我的完整代码。请有人看看并指导我如何修复它以及错误在哪里。

using Word = Microsoft.Office.Interop.Word;
private static void HighlightText()
        {
            object fileName = "D:\\CVArchievePath\\C0000000001.doc";
            object textToFind = "test1";
            object readOnly = true;
            Word.Application word = new Word.Application();
            Word.Document doc = new Word.Document();
            object missing = Type.Missing;
            try
            {
                doc = word.Documents.Open(ref fileName, ref missing, ref readOnly,
                                          ref missing, ref missing, ref missing,
                                          ref missing, ref missing, ref missing,
                                          ref missing, ref missing, ref missing,
                                          ref missing, ref missing, ref missing,
                                          ref missing);
                doc.Activate();

                object matchPhrase = false;
                object matchCase = false;
                object matchPrefix = false;
                object matchSuffix = false;
                object matchWholeWord = false;
                object matchWildcards = false;
                object matchSoundsLike = false;
                object matchAllWordForms = false;
                object matchByte = false;
                object ignoreSpace = false;
                object ignorePunct = false;

                object highlightedColor = Word.WdColor.wdColorGreen;
                object textColor = Word.WdColor.wdColorLightOrange;

                object missingp = false;
                Word.Range range = doc.Range();

                bool highlighted = range.Find.HitHighlight(ref textToFind,
                                                           ref highlightedColor,
                                                           ref textColor,
                                                           ref matchCase,
                                                           ref matchWholeWord,
                                                           ref matchPrefix,
                                                           ref matchSuffix,
                                                           ref matchPhrase,
                                                           ref matchWildcards,
                                                           ref matchSoundsLike,
                                                           ref matchAllWordForms,
                                                           ref matchByte,
                                                           ref missingp,
                                                           ref missingp,
                                                           ref missingp,
                                                           ref missingp,
                                                           ref missingp,
                                                           ref ignoreSpace,
                                                           ref ignorePunct,
                                                           ref missingp);

                System.Diagnostics.Process.Start(fileName.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error : " + ex.Message);
                //Console.ReadKey(true);
            }
        }
4

0 回答 0