locked
Successive InsertFromFile operations lose bookmarks RRS feed

  • Question

  • My add-in adds messages to a Word document. There is a loop that iterates over the messages to be added.

    WriteTextToFile(text, filePath);
    oDoc.Words.Last.InsertFile(filePath, ConfirmConversions: false);

    The method WriteTextToFile writes the message, which is in HTML format to file so that it can be added to the Word document in the next line. Below is the start of a typical message showing that there is an H2 heading which has both an id and an <A> Bookmark.

    <html><HEAD><meta charset='UTF-8'>
    <style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
    </head><body dir="ltr">
    <H2 id="Message 1: 8 Oct 2020  10:13 AM someone@somewhere.org.uk" style="page-break-before: always; font-size:15px; font-family:Calibri,Verdana,sans-serif; text-align: center;"">
     Message 1: 8 Oct 2020  10:13 AM someone@somewhere.org.uk 
     <A name="8_Oct_2020__10_13_AM_someone_somewhere_org"></A>
     </h2><BR>

    This works just fine and produces a Word document with the bookmarks intact.

    The problem comes if the user de-activates the option to show a header. In this case the code produced is pretty much the same but there is no text outside the HTML. Here is an example.

    <html><HEAD><meta charset='UTF-8'>
    <style type="text/css" style="display:none;"> P {margin-margin-bottom:0;} </style>
    </head><body dir="ltr">
    <H2 id="Message 1: 8 Oct 2020  10:13 AM someone@somewhere.org.uk" style="page-break-before: always; font-size:15px; font-family:Calibri,Verdana,sans-serif; text-align: center;"">
      <A name="8_Oct_2020__10_13_AM_someone_somewhere_org"></A>
      </h2>
    <BR>

    So the <H2 id=  > and the <A> bookmark sections are still there, just no text to display as the actual heading.

    What happens then is that the document is produced but only the bookmark for the last message to be added.

    Question: What do I need to do to make sure that every bookmark is accepted when there is no text in the heading? 


    Mike VE

    Friday, October 30, 2020 6:43 PM