Results 1 to 9 of 9
  1. #1
    2 Star Lounger
    Join Date
    Jun 2001
    Location
    York, Pennsylvania, USA
    Posts
    164
    Thanks
    0
    Thanked 3 Times in 2 Posts

    VBA Word Footer (VBA - Word 2007)

    I've got an application that merges data into a document. Most of the time they're one pagers but occasionally two or three pagers. How can I insert a footer on the second and succeeding pages, but not the first via VBA? Thanks.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA Word Footer (VBA - Word 2007)

    Like this (the example is rather simplistic):

    Sub AddFooter()
    With ActiveDocument.Sections(1)
    .PageSetup.DifferentFirstPageHeaderFooter = True
    .Footers(wdHeaderFooterPrimary).Range.InsertAfter _
    Text:="This" & vbTab & "That" & vbTab & "Other"
    End With
    End Sub

  3. #3
    2 Star Lounger
    Join Date
    Jun 2001
    Location
    York, Pennsylvania, USA
    Posts
    164
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Re: VBA Word Footer (VBA - Word 2007)

    As usual, thanks Hans. I'll give it a go.

  4. #4
    2 Star Lounger
    Join Date
    Jun 2001
    Location
    York, Pennsylvania, USA
    Posts
    164
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Re: VBA Word Footer (VBA - Word 2007)

    Hans,

    I've encountered a slight problem that I can't seem to fix. Your code snippet works admirably with one exception. The document I'm working with has a footer on page one. If the document spills over to additional pages, the new footer shows up. Unfortunately, the code appears to wipe out the existing footer on the first page, even if there are no succeeding pages. Any suggestions? Thanks.

    Mort

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA Word Footer (VBA - Word 2007)

    Is this better?

    Sub AddFooter()
    Dim strText As String
    With ActiveDocument.Sections(1)
    ' Retrieve current footer text
    strText = .Footers(wdHeaderFooterPrimary).Range.Text
    .PageSetup.DifferentFirstPageHeaderFooter = True
    ' Set first page footer text to original text
    .Footers(wdHeaderFooterFirstPage).Range.Text = strText
    ' Set other pages footer text
    .Footers(wdHeaderFooterPrimary).Range.Text = "This" & vbTab & "That" & vbTab & "Other"
    End With
    End Sub

  6. #6
    2 Star Lounger
    Join Date
    Jun 2001
    Location
    York, Pennsylvania, USA
    Posts
    164
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Re: VBA Word Footer (VBA - Word 2007)

    I bow to the master. Much appreciated, Hans.

  7. #7
    2 Star Lounger
    Join Date
    Jun 2001
    Location
    York, Pennsylvania, USA
    Posts
    164
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Re: VBA Word Footer (VBA - Word 2007)

    Hans,

    You'll never guess...

    The users like it. But. Now they want the total number of pages tacked on the end.

    Like {PAGES} of {NUMPAGES}. I don't find a method that will do this. Do you have one final suggestion to accomplish that?

    Thanks,

    Mort

  8. #8
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: VBA Word Footer (VBA - Word 2007)

    You need to use something like
    <code>
    Dim rngTemp as Range

    Set rngTemp = .Footers(wdHeaderFooterPrimary).Range
    rngTemp.Collapse Direction:=wdCollapseEnd
    rngTemp.Fields.Add Range:=rngTemp, Type:=wdFieldNumPages
    </code>

    This is completely untested code, just typed in at my laptop, but it should work OK. Put the Dim statement at the beginning and the rest just after the other text has been inserted into the footer.

    StuartR

  9. #9
    2 Star Lounger
    Join Date
    Jun 2001
    Location
    York, Pennsylvania, USA
    Posts
    164
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Re: VBA Word Footer (VBA - Word 2007)

    Thanks, Stuart. That works. I had to fiddle the code a bit from Hans when I added it to get the total pages to show up after the current page number:

    With ActiveDocument.Sections(1)
    strText = .Footers(wdHeaderFooterPrimary).Range.Text
    .PageSetup.DifferentFirstPageHeaderFooter = True
    .Footers(wdHeaderFooterFirstPage).Range.Text = strText
    .Footers(wdHeaderFooterPrimary).Range.InsertAfter Text:=vbTab & Space(20) & "Claim No: " & strNewClaimNumber & vbTab & vbTab & Space(10) & "Page: "
    Set rngTemp = .Footers(wdHeaderFooterPrimary).Range
    rngTemp.Collapse Direction:=wdCollapseEnd
    rngTemp.Fields.Add Range:=rngTemp, Type:=wdFieldPage
    .Footers(wdHeaderFooterPrimary).Range.InsertAfter Text:=" of "
    Set rngTemp = .Footers(wdHeaderFooterPrimary).Range
    rngTemp.Collapse Direction:=wdCollapseEnd
    rngTemp.Fields.Add Range:=rngTemp, Type:=wdFieldNumPages
    End With

    Now if I could find where MS hid the list of wd constants from the VBA help (in Word 2007).

    Thanks Stuart and Hans!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •