none
Issue with footer in word using vba RRS feed

  • Question

  • I want to add footer in a word doc (it may have more than 1 page). The footer should be "Some Text" & Pagenumber.

    For Example: First Page Footer should read: Rob-1, second Pagea Footer should read Rob-2 and so on till the last page. How can we do this using vba?

    Thursday, May 2, 2013 9:38 PM

Answers

  • Try this macro:

    Sub SetFooter()
        Dim rng As Range
        With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
            .Range.Text = vbTab & "Rob-"
            Set rng = .Range
        End With
        rng.Collapse Direction:=wdCollapseEnd
        ActiveDocument.Fields.Add Range:=rng, Type:=wdFieldPage
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by VD Chauhan Friday, May 3, 2013 2:01 PM
    Thursday, May 2, 2013 9:47 PM

All replies

  • Try this macro:

    Sub SetFooter()
        Dim rng As Range
        With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
            .Range.Text = vbTab & "Rob-"
            Set rng = .Range
        End With
        rng.Collapse Direction:=wdCollapseEnd
        ActiveDocument.Fields.Add Range:=rng, Type:=wdFieldPage
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by VD Chauhan Friday, May 3, 2013 2:01 PM
    Thursday, May 2, 2013 9:47 PM
  • Thanks hans! It almost worked! I copied the code and while executing it showed error 13 (mismatch).. I then declared rng as variant and then it worked the way I wanted it to work. Thanks again for the support. Cheers. 
    Friday, May 3, 2013 1:54 PM