none
Image deleted by adding headers and footers in Word RRS feed

  • Question

  • Hi,

    I have an add-in used to add header and footer in the document, if the document header and footer already contain image (watermark), I found that it will be deleted when the following code is run

    Dim tblHeader As Microsoft.Office.Interop.Word.Table = .Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Tables.Add(Range:=.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range, NumRows:=1, NumColumns:=2)

    So may I check any solution can help to keep the image and able to add table in the header and footer?

    Thanks.


    calendarw

    Friday, July 11, 2014 9:24 AM

Answers

  • Hi calendarw

    It helps to understand how RANGES work in Word. When you specify a Range that contains an entire Story (such as a Header: .Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range) anything you assign to the Range will replace current content.

    You can think of it working the same way as when you select text, then type, all the text in the selection is replaced with what you type. And that will include any graphics that are anchored in the Range.

    The trick is to COLLAPSE the Range to either its Start or its End point - think of it like a blinking cursor - so that what you insert will not replace anything. So you could do something like this:

    Dim rngHeader as Word.Range = Document.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
    rngHeader.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    Dim tblHeader As Microsoft.Office.Interop.Word.Table = .Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Tables.Add(Range:=rngHeader, 'etc.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by calendarw Tuesday, July 15, 2014 10:27 AM
    Friday, July 11, 2014 3:42 PM
    Moderator

All replies

  • Hello,

    I'd recommend asking Word-specific questions in the Word for developers forum.

    Anyway, you can use the Macro recorder available in Word and then see the sequence of property and method calls required for getting the job done. See Record or run a macro for more information.

    Friday, July 11, 2014 9:35 AM
  • Hi

    May I check any step can move this to Word forum?  I cannot find it on Edit


    calendarw

    Friday, July 11, 2014 10:01 AM
  • Regular forum users are not allowed to move forum threads. You have to wait for a person who can move your thread or just create a new forum thread there.
    Friday, July 11, 2014 12:36 PM
  • Hi calendarw

    It helps to understand how RANGES work in Word. When you specify a Range that contains an entire Story (such as a Header: .Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range) anything you assign to the Range will replace current content.

    You can think of it working the same way as when you select text, then type, all the text in the selection is replaced with what you type. And that will include any graphics that are anchored in the Range.

    The trick is to COLLAPSE the Range to either its Start or its End point - think of it like a blinking cursor - so that what you insert will not replace anything. So you could do something like this:

    Dim rngHeader as Word.Range = Document.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
    rngHeader.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    Dim tblHeader As Microsoft.Office.Interop.Word.Table = .Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Tables.Add(Range:=rngHeader, 'etc.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by calendarw Tuesday, July 15, 2014 10:27 AM
    Friday, July 11, 2014 3:42 PM
    Moderator
  • Thanks Cindy, it work.

    On the other hand, may I check does it need to ComRelease() for those Range, Tables and HeaderFooters?


    calendarw

    Tuesday, July 15, 2014 10:28 AM
  • Hi calendarw

    That really depends on how your entire project is structured around the "Word interop". There are some people who maintain you should always do so. And there's also the question about whether you should set the object to NOthing, then expressly force garbage collection. You might want to read up on the topic in this article

    http://msdn.microsoft.com/en-us/library/office/aa679807(v=office.11).aspx


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, July 15, 2014 4:41 PM
    Moderator