none
Preserve formatting when replacing bookmarks in Word documents

    Question

  • I need to open an existing word document and replace its bookmarks with values from an app.  The code samples I've reviewed do replace the bookmarks with text, but the formatting is lost.  I also see code examples that apply formatting to paragraphs/runs, but I don't want my app to get into the formatting business.  I'd like to work with the formatting that is explicitly set within the templates (or starting doc file).

    Is there a way to do this?

    Monday, February 11, 2013 7:32 PM

Answers

  • HI PhatStrat

    This is a real "that depends" kind of question. It depends on how the formatting and current text in the bookmarks are organized in the underlying XML.

    If I were in your place, the first thing I'd do would be to take a look at the XML in document.xml in the Zip Package. (Rename the file with a .zip extension, go to the /word folder in the Package and view document.xml.) I'd analyze how the XML looks around and in the bookmarks.

    The markup for a bookmark (start and end) is very "fluid": it can be almost anywhere and other things can intersect. Handling bookmarks can therefore be very complex. And if the document doesn't follow any kind of pattern, it could be very difficult to unravel the XML in such a way that a program can deal with it.

    Can you show us any example of the markup for bookmarks your code should replace?


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, February 12, 2013 8:40 AM
    Moderator

All replies

  • HI PhatStrat

    This is a real "that depends" kind of question. It depends on how the formatting and current text in the bookmarks are organized in the underlying XML.

    If I were in your place, the first thing I'd do would be to take a look at the XML in document.xml in the Zip Package. (Rename the file with a .zip extension, go to the /word folder in the Package and view document.xml.) I'd analyze how the XML looks around and in the bookmarks.

    The markup for a bookmark (start and end) is very "fluid": it can be almost anywhere and other things can intersect. Handling bookmarks can therefore be very complex. And if the document doesn't follow any kind of pattern, it could be very difficult to unravel the XML in such a way that a program can deal with it.

    Can you show us any example of the markup for bookmarks your code should replace?


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, February 12, 2013 8:40 AM
    Moderator
  • As Cindy said, bookmarks are pretty hard to handle because the start and the end tag can wrap almost an arbitrary content (a bookmark can start in the middle of a paragraph inside a table cell and end in the middle of another paragraph). I would suggest you to switch from bookmarks to content controls that are much easier to handle programmatically. You can also check this 3rd party tool/library that uses content controls as placeholders.
    Thursday, February 21, 2013 9:08 AM