none
Find word and hightlight those word in MS-word file issue RRS feed

  • Question

  • i got routine which looks promising. the routine will basically find and highlight text inside ms-word doc file.

    when i run the routine then i am getting error called "Bad variable type. (Exception from HRESULT: 0x80020008 (DISP_E_BADVARTYPE))" i just do not understand how to fix it and what kind of error it is.

    actually i debug and found that when this line bool highlighted = range.Find.HitHighlight() execute then exception occur. here is the full routine

    private static void HighlightText()
        {
            object fileName = "C:\\1.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;
    
                Word.Range range = doc.Range();
    
                bool highlighted = range.Find.HitHighlight(textToFind,
                                                           highlightedColor,
                                                           textColor,
                                                           matchCase,
                                                           matchWholeWord,
                                                           matchPrefix,
                                                           matchSuffix,
                                                           matchPhrase,
                                                           matchWildcards,
                                                           matchSoundsLike,
                                                           matchAllWordForms,
                                                           matchByte,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           ignoreSpace,
                                                           ignorePunct,
                                                           false);
    
                System.Diagnostics.Process.Start(fileName.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error : " + ex.Message);
                Console.ReadKey(true);
            }
        }

    please have a look at the code and guide me what i need to change in code to fix this error but i want to use this function range.find.highlight() . thanks

    Thursday, March 7, 2013 1:16 PM

All replies

  • Hi Mou_kolkata,

    Thank you for posting in the MSDN Forum.

    I've tested your code with a sample document, everything worked fine on my side.

    Could you please provide more detailed information on your machine? Like,

    • System version/bits
    • Office version/bits
    • Visual Studio version.

    It will be very helpful for further troubleshooting.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 8, 2013 10:19 AM
    Moderator
  • i am using win7 32 bit OS, office version is MS office 2007 and VS2010. only this function
    range.Find.HitHighlight()
    throwing exception. please look and tell me the fix. thanks
    Friday, March 8, 2013 2:42 PM