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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Page Numbers to a Word Document

Status
Not open for further replies.

aaronino

Programmer
Aug 6, 2001
19
US
Hi all,

I'm having a hard time figuring out how to add page numbers to a word document. The document is generated just fine, everything is working except that I have not been able to figure out how to put page numbers on it.

I can post some sample code if anyone wants it, but I think it would be slightly confusing because I have some object written to make things easier on me. I'm working with the Word.ApplicationClass though.

Thanks for any help!

-Aaron
 
You'll probably get a better response by asking in the VBA forum.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I'm a little confused as to your answer.

Are you saying that I should ask in the VBA forum because I'll need to do some macro coding in Word, and then run the macro?

Or are you thinking that I'm trying to code this in Word? I'm working in C#. Hmm, here's some sample code.

Code:
WordTool wordReport = new WordTool();

wordReport.CreateNewDocument(filename);  // Creates a new Word Document at the specified location

wordReport.InsertFooter("Copyright © 2004 Fake Company, LLC");
//Insert Footer Method = WordDoc.Sections.Item(1).Footers.Item(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Text = text;			

wordReport.InsertParagraph(this.IntroMain);
// Insert Paragraph Method = CurrentRange.InsertAfter(((char)13).ToString());

wordReport.InsertParagraph(this.IntroMarketValue + "<BR>");
CurrentRange.InsertAfter(((char)13).ToString());

I put the most relevant line from the Insert Paragraph and Insert Footer method. Basically, I'd like to insert page numbers in the header. Does this really fall into the VBA category?
 
JCruz,

Thanks for the links. I actually have already looked at that first page, which allows me to insert the number of pages a document has into a Word Document. It's not treated as a "field", that is, it's just text. If the number of pages were to change, it would not update.

That second link has a tool similar to my "WordTool" that they are selling =). I'm not really interested in picking it up, since I've already developed something very similar.

Regardless, thanks for the links. I'm going to keep plugging away at this problem, I'm sure I'll figure it out sometime.

-Aaron
 
I've start a macro recording and inserted a page number and this what I got:
Selection.Sections(1).Footers(1).PageNumbers.Add PageNumberAlignment:= _
wdAlignPageNumberRight, FirstPage:=True

I hope this is useful.
 
Thank you as well Bobisto. Yesterday I looked into running a macro placed in the normal template for Office, and had some problems there as well. It doesn't seem like the "right" way to do it anyway, but I had issues with the fact that creating the document through automation I couldn't find a way to refer to the "current" document. It's not considered the active document because it's not open, but I also can't open it and run the macro because automation technically has the object open.

I'm putting this on hold again, as I don't like the idea of running it through a macro anyway. I'll figure it out eventually I'm sure, until then, they can just click on insert-> page numbers I guess ;).

-Aaron
 
My idea is not to run a macro. Just to use that code to figure out what line of code to write from C# code. I think that you have to make a small changes.
 
OK, I've got it working (just in case someone else decides to search the threads for something similar).

It's a bit of a hack, as it can require some prep work in MS Office to insert fully custom text. I imagine there's a way you could fully automate it using this method if you dynamically generate the Autotext through automation.

Where WordDoc is the Document object you are working with...

Code:
WordDoc.Sections.Item(1).Headers.Item(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Text = "PAGE"; 
// Inserts the Text "Page" into the header
WordDoc.Sections.Item(1).Headers.Item(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.InsertAutoText();
// Converts the "Page" text into autotext, so that Word recognizes it as a field.

Without any editing of Office, this automation code will give you the following data in the header.

Page X of Y

Where is X = current page and Y = number of pages (the reason for this is that an AutoText entry exists with the name page). To insert custom text, create a new "AutoText" field in Office with the data you wish to display, and name it whatever you like. Replace the word PAGE in the first line of code with the name of your new AutoText entry.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top