locked
IndexOf method in vsto word doc RRS feed

  • Question

  • Hi,

    I need to search text in a Word document from a vsto customization. At present, I'm using a range object to find and process text however I also need to do something like what Eric Lippert does in his VSTO C# book:

    while ((index = text.IndexOf(textToFind, startIndex)) >= 0)

    but I can't access the indexof method from a range object and I don't have a reference to the text of the document as a type string. It doesn't appear that the range object has something like range.text.indexOf().  Eric uses a custom recognizer class along with a recognize method which gives him access to the text variable (which I assume is the entire document) passed in as a param.

    Does anyone know how I can access the full text of a Word document as a type string in order to search text using the indexof method?

    Any ideas or pointing in the general direction is very much appreciated. Thanks.

    Thursday, December 7, 2006 7:14 PM

Answers

  • to answer your question literally, you can pick up all the text in the main body of the document like this:

    string docText = Globals.ThisDocument.InnerObject.Range.Text;

    However, there will be no (easy/direct) way to link this with what's in the document, if you need to do anything with the document content.

    The Text property in the Word object model is what Word defines it to be. And since Word is blissfully ignorant of NET (it's native language being a subset and extension of classic Visual Basic), Word has no idea the anything like IndexOf even exists...

    Thursday, December 7, 2006 9:44 PM

All replies

  • to answer your question literally, you can pick up all the text in the main body of the document like this:

    string docText = Globals.ThisDocument.InnerObject.Range.Text;

    However, there will be no (easy/direct) way to link this with what's in the document, if you need to do anything with the document content.

    The Text property in the Word object model is what Word defines it to be. And since Word is blissfully ignorant of NET (it's native language being a subset and extension of classic Visual Basic), Word has no idea the anything like IndexOf even exists...

    Thursday, December 7, 2006 9:44 PM
  • Thanks as always, Cindy!  your answer lead me to:

    string text = Globals.ThisDocument.InnerObject.Words.ToString()
    while (!((text.IndexOf(textToFind, startIndex)) >= 0))

    I'm still testing and debugging the code block but I think its working.

    Again, thanks very much Cindy.

    Friday, December 8, 2006 4:49 PM