none
Images in Word document Headers RRS feed

  • Question

  • Hi,

    I'm developing a macro for replacing company logo in the headers of the documentation.

    The Word docs have a different header/footer for even/odd pages.

    So, first of all I delete all images in the Sections (I jumped the first one):

            For iSec = 2 To myDocument.Sections.Count
                For Each sShp In myDocument.Sections(iSec).Headers(wdHeaderFooterEvenPages).Shapes
                    sShp.Delete
                Next sShp
                For Each sShp In myDocument.Sections(iSec).Headers(wdHeaderFooterPrimary).Shapes
                    sShp.Delete
                Next sShp
            Next iSec

    Here the first doubts: I was taking for grant that the Shapes were bound to the headers, while I discovered that the Shapes array seems to be independent from their "parent"... Some clarification would be welcome...

    Then I cycled through the Sections, loading and placing the logo in the upper left or right corner depending on the page (even or odd). I differentiated also the upper right position depending on the landscape/portrait orientation.

    Hereafter the code:

            For iSec = 2 To myDocument.Sections.Count
           
                If myDocument.Sections(iSec).PageSetup.Orientation = wdOrientPortrait Then
                    Set rng = myDocument.Sections(iSec).Headers(wdHeaderFooterEvenPages).Range
                    Set Shp = myDocument.Sections(iSec).Headers(wdHeaderFooterEvenPages).Shapes.AddPicture("LogoSmall.jpg", Left:=0, Top:=0, Anchor:=rng)
               
                    Shp.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
                    Shp.Top = CentimetersToPoints(-0.39)
                    Shp.TopRelative = wdShapePositionRelativeNone
                    Shp.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
                    Shp.Left = CentimetersToPoints(0.01)
                    Shp.LeftRelative = wdShapePositionRelativeNone
                   
                    Set rng = myDocument.Sections(iSec).Headers(wdHeaderFooterPrimary).Range
                    Set Shp = myDocument.Sections(iSec).Headers(wdHeaderFooterPrimary).Shapes.AddPicture("LogoSmall.jpg", Left:=0, Top:=0, Anchor:=rng)
                   
                    Shp.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
                    Shp.Top = CentimetersToPoints(-0.39)
                    Shp.TopRelative = wdShapePositionRelativeNone
                    Shp.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
                    Shp.Left = CentimetersToPoints(12.7)
                    Shp.LeftRelative = wdShapePositionRelativeNone
                End If
               If myDocument.Sections(iSec).PageSetup.Orientation = wdOrientLandscape Then
                   
                    Set rng = myDocument.Sections(iSec).Headers(wdHeaderFooterEvenPages).Range
                    Set Shp = myDocument.Sections(iSec).Headers(wdHeaderFooterEvenPages).Shapes.AddPicture("LogoSmall.jpg", Left:=0, Top:=0, Anchor:=rng)
               
                    Shp.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
                    Shp.Top = CentimetersToPoints(-0.39)
                    Shp.TopRelative = wdShapePositionRelativeNone
                    Shp.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
                    Shp.Left = CentimetersToPoints(0.01)
                    Shp.LeftRelative = wdShapePositionRelativeNone
     
                    Set rng = myDocument.Sections(iSec).Headers(wdHeaderFooterPrimary).Range
                    Set Shp = myDocument.Sections(iSec).Headers(wdHeaderFooterPrimary).Shapes.AddPicture("LogoSmall.jpg", Left:=0, Top:=0, Anchor:=rng)
                   
                    Shp.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
                    Shp.Top = CentimetersToPoints(-0.39)
                    Shp.TopRelative = wdShapePositionRelativeNone
                    Shp.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
                    Shp.Left = CentimetersToPoints(21.88)
                    Shp.LeftRelative = wdShapePositionRelativeNone
               End If
               
            Next iSec

    I know that I could have differentiated the code just for the upper right position, instead of writing two almost identical section of code depending on the page orientation... But in that way the code behaviour seems to be unpredictable: most of the logos are placed in the wrong header (or footer!).

    In this way everything seems to be right but... in some case I got (random?) strange behaviour. For example, when the section changes and the orientation becomes from portrait to landscape, the first logo of the landscape page is placed in the footer of the previous Section.

    But the strange thing is that if I place a "break" on the instruction that loads the image and execute the macro, everything works fine! The code is executed, it stops at the break, I press "F5", the code continues, and everything is correct...

    So, what am I doing wrong? This behaviours are normally associated to strange memory allocations (I was a former C developer ;)). I tried also to put a "sleep" in order to slow down the execution, but it didn't work...

    Any suggestion (and of course correction)?

    I know that my programming style is a little bit confusing... But I have a lot of doubts on the VBA object structure representing a document and their parts...

    Thank you!

    Francesco

    Friday, January 22, 2016 6:01 PM