0
votes

I would like to be able to add the page numbers in a word document to the header.

I have found some links that display the code in visual basic, but no matter what i try, i keep getting errors. It keeps telling me the either "sections" or "headers" or "footers" is not a supported automation object.

Any help would realy be great. Thank You.

Links:

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

http://msdn.microsoft.com/en-us/library/office/ff194253(v=office.14).aspx

I Tried addapting the link's code with no success:

wrdDoc.ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).PageNumbers.Add

and if possible please how to allign the page number right.

1
"I keep getting errors" is not very helpful. Please give a short but complete sample of code, and verbatim error messages.David Heffernan
@davidheffernan the answer was already given. I also couldn't figure it out. But thanks anywayuser3510818
I'm giving you general advice. Next time you ask a question please try to include such details.David Heffernan

1 Answers

1
votes

Use [] instead of () for indexes

//Headers[wdHeaderFooterPrimary] = Headers[1]

//Add possible values:
//wdAlignPageNumberCenter = 1
//wdAlignPageNumberInside = 3
//wdAlignPageNumberLeft = 0
//wdAlignPageNumberOutside = 4
//wdAlignPageNumberRight =2

wrdDoc.Sections[1].Headers[1].PageNumbers.Add(1); //example with center alignment
wrdDoc.Sections[1].Headers[1].PageNumbers.Add(2); //example with right alignment