We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

insert page numbers automatically to word documents using c#

pessi
pessi asked
on
Medium Priority
3,258 Views
Last Modified: 2013-12-17
Dear All,

I need 2 things.
1. Adding page numbers to existing word document using c#
2. Adding Text at the top of the first page.

Is that possible? can someone help please?

prasad.
Comment
Watch Question

Commented:
Hi Pessi
Off course - it is possible.
You can see how VB doing that in Word.
Start to record MACRO in word, do what you want (page numbers etc) and then go to VB in Word to see what commands are recorded by your macro. You will need to translate from VB to C# but it's easy.

Regards
  Daniel

Author

Commented:
Daniel

Thank you for the answer. I did that...
and got this..

Sub Macro1()
    Selection.Sections(1).Footers(1).PageNumbers.Add PageNumberAlignment:= _
        wdAlignPageNumberRight, FirstPage:=True
End Sub

I dont know any VB can you please help me give a c# equivalent?

Thanks
Prasad

Commented:
First - do you have any lines of code in C# which doing anythin on word doc? If yes - put it here. If no then I will prepare some lines for you.

Author

Commented:
here is my code

Word.Application app = new Word.ApplicationClass();
Word.Document doc = new Word.Document();
       doc = app.Documents.Open(ref fileToOpen,
                    ref Missing, ref Missing, ref Missing, ref Missing,
                    ref Missing, ref Missing, ref Missing, ref Missing,
                    ref Missing, ref Missing, ref Missing, ref Missing,
                    ref Missing, ref Missing, ref Missing);

// I for example change the font size of the full document text
doc.Content.Font.Size = int.Parse(txtFontSize.Text);

I want to do something here..that puts page numbers into the document.

//Finally save and close.

doc.SaveAs(ref fileToSave,
              ref Missing, ref Missing, ref Missing, ref Missing,
              ref Missing, ref Missing, ref Missing, ref Missing,
              ref Missing, ref Missing, ref Missing, ref Missing,
              ref Missing, ref Missing, ref Missing);
 doc.Close(ref Missing, ref Missing, ref Missing);
  app.Quit(ref Missing, ref Missing, ref Missing);

Prasad


Author

Commented:
any idea anyone?

Commented:
Objection
My answer was given in topic area and it could help Pessi.
I've answered before with very similar question and my answer was correct.
Daniel

Author

Commented:
Daniel

for me personally your answer wasnt sufficient. You told me to record a macro. I already knew that and Did that..
The macro code gave me no hint at all. I rather solved it using another technique.
Points is not a point for me, so if you feel you should get the points, I will give them to you,
but personally I didnot benefit from your answer.

Prasad.

Author

Commented:
here was the solution.
//SETTING THE FOCUES ON THE PAGE FOOTER

oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter;

 

//ENTERING A PARAGRAPH BREAK "ENTER"

oWord.Selection.TypeParagraph();

 

String docNumber = "1";

String revisionNumber = "0";

 

//INSERTING THE PAGE NUMBERS CENTRALLY ALIGNED IN THE PAGE FOOTER

oWord.Selection.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;

oWord.ActiveWindow.Selection.Font.Name = "Arial";

oWord.ActiveWindow.Selection.Font.Size = 8;

oWord.ActiveWindow.Selection.TypeText("Document #: " + docNumber + " - Revision #: " + revisionNumber);

 

//INSERTING TAB CHARACTERS

oWord.ActiveWindow.Selection.TypeText("\t");

oWord.ActiveWindow.Selection.TypeText("\t");

 

oWord.ActiveWindow.Selection.TypeText("Page ");

Object CurrentPage = Word.WdFieldType.wdFieldPage;

oWord.ActiveWindow.Selection.Fields.Add(oWord.Selection.Range, ref CurrentPage, ref oMissing, ref oMissing);

oWord.ActiveWindow.Selection.TypeText(" of ");

Object TotalPages = Word.WdFieldType.wdFieldNumPages;

oWord.ActiveWindow.Selection.Fields.Add(oWord.Selection.Range, ref TotalPages, ref oMissing, ref oMissing);

 

//SETTING FOCUES BACK TO DOCUMENT

oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
Commented:
It's OK
I haven't time to finish your question - I was busy last week.If you did that earlier than my final answer then it's OK. I don't need your points at all. For another person which I helped before here at e-e that trick with MACRO was helpfull.
Daniel

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Daniel

Sorry if I offended you. My intention wasnt that.
I heartfully thank you for trying to help me out.

Thanks again.
Prasad.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.