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