开发者

Word Interop: setting doc.Bookmarks("BookmarkName").Range.Text causes watermark and other elements of template to disappear from document

I am automating Word from VB.Net.

I open a document by:

Dim msWord as Word.Application = CreateObject("Word.Application")
Dim doc As Word.Document = msWord.Documents.Add(Template:=Path)

where path points to a template file I created with a header and a watermark. The template has some bookmarks which I want to dynamically set to some appropriate text values:

doc.Bookmarks("DocumentTitle").Range.Text = "The Joy of Office Automation"

If I comment out that line, the document opens in Word with the watermark, etc, in place. If I let that line execute, it inserts the text as appropriate, but the waterma开发者_StackOverflowrk and other things disappear from the document. You can actually see it flash briefly, and then disappear.

FWIW, this is Office Word 2007, and I am opening a .Dot (Word 97-2003) template. The Bookmark.Range.Text I am setting is in the document header.

Office Automation gurus, what am I doing wrong?

Thanks, Gerald


My first thought is maybe somehow you've got the watermark associated with the range that the bookmark "marks". If you then replace that range with some other text, pop! no more watermark.

But that seems like a longshot because I don't think watermarks are normally associated with ranges.

Is it possible the bookmark spans a section break? If the watermark is set to a specific section and replacing the bookmark wipes out the section break, you'll loose everything about that section (including possibly headers, footers, margins etc).


I have discovered some more information: The watermark only disappears if the bookmark in question is in the very first position within the document header. Setting any other bookmark within the document does not cause the watermark to disappear. Adding even a single space before the bookmark in question will prevent the watermark from disappearing when the range text is set.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜