Full Version: Use Access Vba To Write The Footer Of Word Doc Like Page X Of Y
UtterAccess Forums > Microsoft® Access > Access Automation
I have been working on this for 2 days and am really frustrated. Need help.
I am trying to insert the page number in the footer of a newly created document. I want it to look like Page x of y. I can write text to the header and footer already. I just can not insert the field for the Page number and Numer of Pages in the footer. I can write "Page" but the numbering won't go. Can anyone please help? Here is where I am now:
Set Doc = word.documents.Add
With Doc '.documents.Sections(1)
.Sections(1).Headers(wdHeaderFooterPrimary).Range.Bold = True
.Sections(1).Headers(wdHeaderFooterPrimary).Range.Paragraphs.Alignment = wdAlignParagraphCenter
.Sections(1).Headers(wdHeaderFooterPrimary).Range.text = (strHeader)
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs.Alignment = wdAlignParagraphCenter
.Sections(1).Footers(wdHeaderFooterPrimary).Range.text = .Sections(1).Footers _
(wdHeaderFooterPrimary).Range.Fields.Add(Range:=Selection.Range, Type:=wdFieldAutoNum, _
text:="Page", PreserveFormatting:=True) & " of " & .Sections(1).Footers(wdHeaderFooterPrimary) _
.Range.Fields.Add(Range:=Selection.Range, Type:=wdFieldNumPages, text:="Pages ", PreserveFormatting:=True)
End With
I have also tried this:
Dim oRng As Range

With Doc.Fields
.Add Range:=Selection.Range, Type:=wdFieldPage
.Sections(1).Footers(wdHeaderFooterPrimary).Range.InsertBefore text:="Page "
.Collapse Direction:=wdCollapseEnd
.Sections(1).Footers(wdHeaderFooterPrimary).Range.InsertAfter (" of ")
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs(1).Alignment = wdAlignParagraphRight
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Collapse Direction:=wdCollapseEnd
.Add Range:=Selection.Range, Type:=wdFieldNumPages
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Start = .Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs(1).Range.End
.Sections(1).Footers(wdHeaderFooterPrimary).Range.End = .Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs(1).Range.End
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs(1).Alignment = 1 'wdAlignParagraphCenter
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Collapse Direction:=wdCollapseEnd
End With
The second code will not get me past the .InsertBefore as I get an error message "Object does not support this Property or Method"
Any help would be great. I have looked everywhere.
Just for reference I am usign XP and Access 2003.
You should review:
Another approach is simply record a macro and insert the page footer you want and look over the resulting code. A great way to learn!
Thanks much. Tried that. the line [ rng.InsertBefore vbTab & "Page of "] will not run. Msg = method or property not supported. Any thoughts?
Thanks for the references. I have tried all 3 locations and have not gotten them to work. The first gives me only the page number which I can do. The second gives me an error on "InsertBefore" line (I think this is VBA in Word and not in Access). The third one uses this .TypeText Text:=" of " and that writes the text in the footer because it is the last thing written in the footer so that is all I see.
Any other ideas? Anyone?????
Thanks Daniel, We figured it out from the references you sent and some work we had already done. Everything works fine now. We appreciate you help. We can insert formatted page numbers such as "-1-" into the footer but are still not able to insert page numbers with text such as "Page 1". Any ideas on that? You help was great. QTip and LaCrestaCowboy. We live and work in Calif and will need some consulting soon. if interested reply to lacrestacowboy[AT]gmail[DOT]com
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.