SPHUDSON

I am trying to change some text in the header of a Word document. Below is my code:

PHP Code:
wordQuoteApp = New Word.Application()

wordQuoteApp.Documents.Open(filename:=strPath, readonly:=True)
wordQuoteDoc = wordQuoteApp.ActiveDocument

With wordQuoteApp
.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader

.ActiveWindow.ActivePane.Selection.GoTo(What:=Word.WdGoToItem.wdGoToBookmark, Name:="HeaderCustomerName")
.
ActiveWindow.ActivePane.Selection.TypeText(Text:=txtCompanyName.Text)

End With

I keep getting the error that it can't find the book mark even though I know it is there. I use the same book mark code in the body of the Word document and it works just fine.

Any help would be appreciated



Re: Visual Studio Tools for Office Word Automation via VB.net

Cindy Meister

Automation questions should be directed to the office.developer.automation newsgroup, or the word.vba.general newsgroup. This forum is specifically for VSTO-related questions (which doesn't not comprise automation of Office applications).

If you do a Google (or similar) search on these groups you'll find any number of discussions around accessing ranges in the document header and footer. The main point you'll see in all these discussions is: avoid using "Selection"; work directly with Range objects, instead. Not only is this more accurate, it's also faster and you'll have less screen flicker. An example, based on your code snippet:

wordQuoteDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Bookmarks("HeaderCustomerName").Range.Text = txtCompanyName.Text

There are alternatives to using bookmarks, if you don't expect the user to edit the text. For example, you can place the text in a document variable (not to be confused with a variable in your code) or a document property. Then insert a DocProperty or DocVariable field in the document where you want to display this text.

Enjoy your futher research in the newsgroups