MS-Word API method Find.HitHighlight is throwing exception

1

i am trying to find and highlight text in word file progrramatically but when the line bool highlighted = range.Find.HitHighlight execute then a exception is occuring and the exception message is Bad variable type. (Exception from HRESULT: 0x80020008 (DISP_E_BADVARTYPE))

i am not being able to find out the reason what causes this error message. here is my full code. please some one have a look and guide me how to fix it and where is the error.

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);
            }
        }
c#
ms-word
find
highlight
asked on Stack Overflow Mar 8, 2013 by Thomas

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0