Home > Software engineering >  Insert header on second page of a word template, while the second page doesn't exist yet
Insert header on second page of a word template, while the second page doesn't exist yet

Time:12-15

I need to create a Word-Template, which dynamically adds a logo into the header of a letter. To simplify the user experience, the original template gets generated with only one page. The process goes like this:

  1. The user opens the template via "Document>New" in Word
  2. The user can choose between different companies and logos via a UserForm
  3. Depending on the choice of company/logo, different content is added to the letter

I do have a constraint, that the footer on the first page is different to the ones from the second page onwards.

This means, that when I add a picture into the header on the first page via VBA code and I add text (or just blank space), the logo doesn't get repeated.

So basically the question I have is, if I can add a picture to a header from the second page onward, while the page doesn't exist yet?

My code at the moment is something like the following, although I have removed the parts for image formating, since it isn't relevant in my opinion:

With ActiveDocument
 .Sections(1).Footers(wdHeaderFooterFirstPage) _ 
 .Range.InlineShapes.AddPicture(path)
End With

I have also tried to preemptively add the header to the following pages, but it doesn't seem to work

With ActiveDocument
 .Sections(1).Footers(wdHeaderFooterFirstPage) _ 
 .Range.InlineShapes.AddPicture(path)
End With

With ActiveDocument
 .Sections(1).Footers(wdHeaderFooterPrimary) _ 
 .Range.InlineShapes.AddPicture(path)
End With

CodePudding user response:

I've included two statements to place a picture. Either will work:

Sub AddGraphicPage2()
  Application.ScreenUpdating = False
  Dim aSection As Section
  For Each aSection In ActiveDocument.Sections
    With aSection
      .Headers(wdHeaderFooterPrimary).Shapes.AddPicture FileName:="C:\picture.png"
      .Headers(wdHeaderFooterPrimary).Range.InlineShapes.AddPicture FileName:="C:\picture.png"
    End With
  Next aSection
  Application.ScreenUpdating = True
End Sub

Checking all sections makes this a general-purpose macro that will also work with more complex documents than a single page.

CodePudding user response:

I figured out, why the image was "moving" to page 2, when I added a second (and third and so on) page. It had to do with the way I was adding the image.

Originally I was defining the anchor, where I attached the image, as Anchor:=Selection.Range. I was just using code, which I knew to work with the format of the images.

So I just removed the anchor and now the image gets attached properly in each header, no matter if on page 1 or 2 or 13.

See below, the code, that worked for me.

I did, of course, implement it properly in a function, which I call, when I need it ;-)

ActiveDocument.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True

' Insert image in header for page 1

    With ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)
        With .Shapes.AddPicture(FileName:=filename, LinkToFile:=False, SaveWithDocument:=True)
            .name = "Logo_Page1"
            .Width = CentimetersToPoints(21)
            .Height = CentimetersToPoints(3)
            .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
            .Left = CentimetersToPoints(0)
            .RelativeVerticalPosition = wdRelativeVerticalPositionPage
            .Top = CentimetersToPoints(0)
            .WrapFormat.Type = wdWrapBehind
        End With
    End With
    
' Insert image in header from page 2 onwards
    
    With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
        With .Shapes.AddPicture(FileName:=filename, LinkToFile:=False, SaveWithDocument:=True,Anchor:=Selection.Range)
            .name = "Logo_Page2"
            .Width = CentimetersToPoints(21)
            .Height = CentimetersToPoints(3)
            .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
            .Left = CentimetersToPoints(0)
            .RelativeVerticalPosition = wdRelativeVerticalPositionPage
            .Top = CentimetersToPoints(0)
            .WrapFormat.Type = wdWrapBehind
        End With
    End With
  • Related