Is there a way to add a header and footer to a Word document using VBA, as described below?
The header should be a combination of:
- "New Items Report" in bold 16 pt font that is left-justified, and
- The current date and time in non-bold, 12 pt font that is right-justified. (I defined a myDateTime variable in the code below to use for the date/time string.)
The footer should be a centered "Page X of Y" (where X and Y are fields for the page # and # of pages, respectively).
I know well how to do this manually in Word, but despite many Google searches can't quite crack this (or really figure out where to start) using VBA. Below is Access VBA code that I have currently to create the document.
Appreciate any solutions or pointers.
' declare vars and set up
Dim objWord As Word.Application
Dim doc As Word.Document
Dim WordHeaderFooter As HeaderFooter
Dim myDateTime As String
' variable to be used as Date/Time in header
myDateTime = Format(Now(), "Long Date") & " " & Format(Now(), "Medium Time")
Set objWord = CreateObject("Word.Application")
' create doc and insert sample text
With objWord
.Visible = True
Set doc = .Documents.Add
doc.SaveAs CurrentProject.Path & "\TestDoc.doc"
End With
With objWord.Selection
.Font.Name = "Calibri (Body)"
.Font.Size = 12
.TypeText "Here is an example line of text."
End With
doc.Save
doc.Activate