Link to home
Start Free TrialLog in
Avatar of pessi
pessi

asked on

insert page numbers automatically to word documents using c#

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.
Avatar of DanSo1
DanSo1
Flag of Poland image

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
Avatar of pessi
pessi

ASKER

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
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.
Avatar of pessi

ASKER

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


Avatar of pessi

ASKER

any idea anyone?
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
Avatar of pessi

ASKER

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.
Avatar of pessi

ASKER

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;
ASKER CERTIFIED SOLUTION
Avatar of DanSo1
DanSo1
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pessi

ASKER

Daniel

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

Thanks again.
Prasad.