Full Version: Use Access Vba To Write The Footer Of Word Doc Like Page X Of Y
UtterAccess Discussion Forums > Microsoft® Access > Access Automation
QTip
I have been working on this for 2 days and am really frustrated. Need help.
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.
Thanks.
Just for reference I am usign XP and Access 2003.
DanielPineault
You should review:
a href="http://blogs.technet.com/b/heyscriptingguy/archive/2006/05/10/how-can-i-add-centered-page-numbers-to-the-footer-of-a-word-document.aspx" target="_blank">http://blogs.technet.com/b/heyscriptingguy...d-document.aspx
http://www.experts-exchange.com/Software/O...Q_23467589.html
http://www.java2s.com/Code/VBA-Excel-Acces...PageNumbers.htm
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!
QTip
Thanks much. Tried that. the line [ rng.InsertBefore vbTab & "Page of "] will not run. Msg = method or property not supported. Any thoughts?
QTip
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.
ny other ideas? Anyone?????
QTip
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 our main content. To view the full version with more information, formatting and images, please click here.