none
problem with Word Interop: setting bookmark.range.text causes document watermarks to disappear. RRS feed

  • Question

  • I am trying to open a document from a template and then set text dynamically using word interop 12.0 in VB.Net framework 4.0.  This part works.  But the template has a watermark, and at the point that the text is set for any bookmark, the watermark disappears from the document:

     

    Dim doc As Word.Document = msWord.Documents.Add(Template:=PathToWord2003Template)

    doc.Bookmarks(

     

    "myBookmark").Range.Text = "The Joy of Office Interop" 'watermark disappears when this line executes

      

    I am new at using interop, so if I am totally offbase with what I am trying to do, I am open to suggestions.

    Thanks,

    Gerald

     

    Friday, May 20, 2011 11:57 AM

Answers

  • Hi Gerald,

    You might try this. Instead of using Word's insert Watermark, make your own using SmartArt text and insert it in the Header(s) of the document's various sections (if they're not linked) as needed.

    Then try your bookmarking and see if that works and hopefully it doesn't remove the custom watermark.


    Wag and they wag with you, Howl and they put you outside. http://greatcirclelearning.com
    Friday, May 20, 2011 1:50 PM
  • When you replace the range of a bookmark, what was there before disappears. Watermarks are graphics anchored in Headers, so it sounds like your watermark graphic is anchored inside your bookmark and is getting replaced. Make sure the watermark is anchored outside the bookmark.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Saturday, May 21, 2011 8:42 AM

All replies

  • Sorry for the boogered up code example:

    Dim doc As Word.Document = msWord.Documents.Add(Template:=PathToWord2003Template)

    doc.Bookmarks(

     

    "myBookmark").Range.Text = "The Joy of Office Interop" 'watermark disappears when this line executes

      

     

    Friday, May 20, 2011 11:58 AM
  • <sigh>

     

    Friday, May 20, 2011 11:58 AM
  • OK, I've discovered that it only happens when the bookmark is at the very first position in the document... in this case it is in the header.  Setting other bookmarks that are not in that position do not cause the bookmark to disappear.

    G

    Friday, May 20, 2011 12:34 PM
  • Hi Gerald,

    You might try this. Instead of using Word's insert Watermark, make your own using SmartArt text and insert it in the Header(s) of the document's various sections (if they're not linked) as needed.

    Then try your bookmarking and see if that works and hopefully it doesn't remove the custom watermark.


    Wag and they wag with you, Howl and they put you outside. http://greatcirclelearning.com
    Friday, May 20, 2011 1:50 PM
  • When you replace the range of a bookmark, what was there before disappears. Watermarks are graphics anchored in Headers, so it sounds like your watermark graphic is anchored inside your bookmark and is getting replaced. Make sure the watermark is anchored outside the bookmark.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Saturday, May 21, 2011 8:42 AM
  • Hi mooreg,

    Have you sovled the problem? Have you look at the above replies which are helpful to you.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 25, 2011 1:00 PM
    Moderator