Use bookmarks to locate the specified location of a Word document (three methods)
First, set and name the bookmarks in the Word documents. (assume that a bookmark named "bm_test" is created)
Then use C # To operate the word
Using MSWord = Microsoft. Office. InterOP. word;
Private MSWord. Application wordapp; // word ApplicationProgramVariable
Private MSWord. Document worddoc; // Word document variable
Private object nothing = missing. value;
// Initialization
Wordapp = new MSWord. applicationclass ();
Worddoc = wordapp. Documents. Add (ref nothing, ref nothing );
// Open Word
Object filename = strpath;
Object readonly = false;
Object isvisible = true;
Worddoc = wordapp. Documents. Open (ref filename, ref nothing, ref readonly,
Ref nothing, ref nothing,
Ref nothing, ref isvisible, ref nothing,
Ref nothing, ref nothing, ref nothing );
Object BK = "bm_test ";
Method 1: use the word application variable. In this method, make sure that the isvisible value is true in wordapp. Documents. open ().
If (wordapp. activedocument. bookmarks. exists ("bm_test "))
{
Wordapp. activedocument. bookmarks. get_item (ref BK). Select ();
Wordapp. selection. typetext ("insert text"); // insert text
}
Method 2: Use Word document Variables
If (worddoc. bookmarks. exists ("bm_test "))
{
Worddoc. bookmarks. get_item (ref BK). range. Text = "insert text"; // insert text
}
Method 3: Use the Goto function to jump to the specified bookmarks
Object bookmarkname = "bm_test ";
Object what = MSWord. wdgotoitem. wdgotobookmark;
Worddoc. activewindow. selection. Goto (ref what, ref nothing, ref nothing, ref bookmarkname );
Worddoc. activewindow. selection. typetext ("Hello! ");