Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[Word VBA] Adding Footer with Pagenumber / Printdate to Document

Status
Not open for further replies.

njitter

Technical User
Sep 4, 2001
122
0
0
US
Hello,

i'm trying to add a Footer containg Pagenumber/Total pages and Printdate to a document when it is created.
I want the Printdate to be left-aligned and the Pagenumber/Totalpages to be right-aligned.

Excel has .Leftfooter and .Rightfooter but these don't work in Word :(

Could someone give me a hint on where to start?

---
It's never too late to do the Right thing
 
Hi njitter,

In Word, you'd normally use a:
'PAGE' field for the page number,
'NUMPAGEs' field for the number of pages, and
'PRINTDATE' field for the print date.
No vba is required, unless you have a particular reason for adding these fields programmatically.

To get to the header and/or footer, where you'd normally place these fields, select View|Header and Footer from the menu.


Where you position these is a matter of footer patagraph formatting, the same as for any other paragrpahs in the document.

Cheers
 
Hello Macropod,

i need to add these fields programmatically. The document is created from within a Visual Basic program.



---
It's never too late to do the Right thing
 
Hi njitter,

In that case, check out Word's Add Method (Fields Collection) and HeaderFooter Object.

However, if you create the document from a template with the fields already in it and laid out as per your requirements, then there's still no need to add them programatically afterwards.

Cheers
 
The page is not created from a template. It's a piece of software i did not code myself. I just need to make some modifications to it (original company stopped supporting it)



---
It's never too late to do the Right thing
 
Hi njitter,

OK, give this a go:
Code:
Sub AddFooterFields()
Dim oSection As Section
Dim oHeadFoot As HeaderFooter
For Each oSection In ActiveDocument.Sections
    For Each oHeadFoot In oSection.Footers
        If Not oHeadFoot.LinkToPrevious Then
            oHeadFoot.Range.Select
            With Selection.Fields
                .Add Range:=Selection.Range, Type:=wdFieldPrintDate, _
                 Text:="\@ " & Chr(34) & "d, dddd MMMM yyyy" & Chr(34), PreserveFormatting:=False
                With Selection
                    .Collapse (wdCollapseEnd)
                    .TypeText Chr(9) & Chr(9) & "Page "
                End With
                .Add Range:=Selection.Range, Type:=wdFieldPage, PreserveFormatting:=False
                With Selection
                    .Collapse (wdCollapseEnd)
                    .TypeText " of "
                End With
                .Add Range:=Selection.Range, Type:=wdFieldNumPages, PreserveFormatting:=False
            End With
        End If
    Next
Next
If ActiveWindow.View.SplitSpecial = wdPaneNone Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
Else
    ActiveWindow.View.Type = wdPrintView
End If
End Sub
You may need to change the date formatting (shown as 'd, dddd MMMM yyyy' above) to suit your needs.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top