V G S Naidu A

V G S Naidu A

  • NA
  • 63
  • 18.3k

How to search the text in WebBrowser Control

Dec 20 2012 7:35 AM
Hai,
 we have a requirement like previewing the documents as like in windows7/vista
"preview Pane" and have to implement the search functionality in Winforms.For that,
 I have loaded the Ms office docuemnts, and Excel Sheets and Power Point documents into Webbrowser Control in html format.Now we need to implement the search functionality in that webbrowser Control.
 
For that we have implemented the search functionality using the following code 

 public bool FindNext(string text, WebBrowser webBrowser1)
        {


            IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
   
            IHTMLSelectionObject sel =doc.selection;// (IHTMLSelectionObject)doc.selection;
            IHTMLTxtRange rng = sel.createRange() as IHTMLTxtRange;
       
            rng.collapse(false); // collapse the current selection so we start from the end of the previous range
            if (rng.findText(text, 1000000, 0))
            {
                rng.select();
                return true;
            }
            else
                FindFirst(text, webBrowser1);
            return false;
        }

This code is working fine for searching the string values in word Document for all the occurences.
   But when it Comes to Excel and PPt documents this code doesn't work properly.it finds only the first occurrence of the string other occurrences doesn't find.
  while debugging i found that,for word Documents "IHTMLDocument2" object stores the html Content in "innerHTML" and "innerText" with some values.
But for Excel it stores the text in "innerHTML" only by using the Frames, and sheets are referenced to local temporary .html files and it didn't have any "innerText" content it showing as null.
 
      Please provide the solution to search the text in webbrowser control which loaded the html content, that is converted from Excel,PPT to html type and displaying in webbrowser control.

If you have any queries please feel to ask me.

Thank You.