.net MS Word Interop - Find text and return a Word.Range
Posted on 2008-10-24
What I'm trying to do is take Indentifiers that I type into a word doc and set bookmarks on them.
Say I have a Word Doc with the following text
"The Raven" is a narrative poem by the American <Goody> Edgar Allan Poe, first published in January 1845. It is noted for its musicality, <HereIAm> language, and supernatural atmosphere. It tells of a talking raven's mysterious visit to a distraught lover, <AnotherWord> the man's slow descent into madness. The lover, often identified as being a student, is lamenting the loss of his love, Lenore. The raven, sitting on a bust of Pallas, seems to further instigate his distress with its constant repetition of the word, "Nevermore". Throughout the poem, Poe makes allusions to folklore and various classical works.
I Would like to use something like a Regex to find strings formated like <[SomeText]> and then create a bookmark over <[SomeText]>. IE, in the above paragraph I would create bookmarks fro <AnotherWord>, <Goody>, And <HereIAm>
To do this using Word, I would highlight the <[SomeText]> then goto Insert->Bookmark.
I believe I can figure out how to add the bookmark as long as I get can a Range object that covers the text range of the <[SomeText]> identifier. I've tried running a regex on the Word.Content.Text using Text.RegularExpression.Regex and I get find the matches but when I try to create a range from the starting index of the match and the index of the last character its not selecting the right text.
Another problem that I noticed, is if Word.Content.Text is used, it does not return text that is inside a head or footer. I will also need code that can find these identifiers within header and footer fields.