• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2553
  • Last Modified:

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.
0
pessi
Asked:
pessi
  • 6
  • 4
1 Solution
 
DanSo1Commented:
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
0
 
pessiAuthor 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
0
 
DanSo1Commented:
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.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
pessiAuthor 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


0
 
pessiAuthor Commented:
any idea anyone?
0
 
DanSo1Commented:
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
0
 
pessiAuthor 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.
0
 
pessiAuthor 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;
0
 
DanSo1Commented:
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
0
 
pessiAuthor Commented:
Daniel

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

Thanks again.
Prasad.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now