Mikk
Mikk

Reputation: 331

How to add picture into Word Header which contains already a table?

I am trying to add a picture (company logo) into a header by code. This worked fine so far until there showed up some documents which contain a table in the header, which I want to keep there too.

Problem is: my code adds the picture into the first table cell. What i want is that the picture is positioned in the top right corner of the page (with some margin to the page) .. but outside the table.

How do I need to modify my code to do that? I guess the problem is the Range I use:

    Set oSec = ActiveDocument.Sections(1)
    Set oHeader = oSec.Headers(wdHeaderFooterFirstPage)
    Set Rng = oHeader.Range '<<-- Problem here? What to do if there is a table in the header
    Set sh = ActiveDocument.shapes.AddPicture(LogoFile, False, True, 0, 0, , , Rng)
    With sh
        .Height = LogoDimension
        .Width = LogoDimension
        .WrapFormat.Type = wdWrapTopBottom
        .WrapFormat.Side = wdWrapTopBottom
        .WrapFormat.DistanceBottom = MillimetersToPoints(10)
        .RelativeHorizontalPosition = wdRelativeHorizontalPositionRightMarginArea
        .RelativeVerticalPosition = wdRelativeVerticalPositionPage
        .Left = MillimetersToPoints(0.5) - LogoDimension
        .Top = MillimetersToPoints(11.5)
    End With

Thanks for any hints!

Upvotes: 0

Views: 1753

Answers (2)

Cindy Meister
Cindy Meister

Reputation: 25663

I was able to test the scenario on my dev machine and could reproduce the problem. Shape management in Word's Headers/Footers is notorious for being "pliable" - this appears to be another one of those things.

What works is to insert the graphic in the paragraph below the table as an InlineShape object, use the ConvertToShape method, then immediately lock the anchor of the Shape so that moving it doesn't shift the anchor position to the nearest paragraph (table cell).

Sub InsertPicInHeaderOutsideTable()
    Dim oSec As word.Section
    Dim oHeader As word.HeaderFooter
    Dim rng As word.Range
    Dim sh As word.Shape, ils As word.InlineShape

    Set oSec = ActiveDocument.Sections(1)
    Set oHeader = oSec.Headers(wdHeaderFooterFirstPage)
    Set rng = oHeader.Range 
    '**** Add the followign four lines to code in your question ****
    rng.Collapse wdCollapseEnd
    Set ils = rng.InlineShapes.AddPicture(LogoFile, False, True, rng)
    Set sh = ils.ConvertToShape
    sh.LockAnchor = True
    With sh
       'and so on...

Upvotes: 1

Mikk
Mikk

Reputation: 331

after some more playing around with it I found the solution:

                    .LayoutInCell = False

adding this attribute to the shape will lead to positioning the picture as wanted and not affect the table anymore.

edit: this was not entirely correct it seems. The picture is still added to the table .. just not positioned in the cell anymore. If I delete the table for testing the picture gets deleted automatically with it. So this is not an ideal solution still. I think still the range used is the problem

Upvotes: 0

Related Questions