?
Solved

insert page numbers automatically to word documents using c#

Posted on 2009-02-17
10
Medium Priority
?
2,342 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.
0
Comment
Question by:pessi
  • 6
  • 4
10 Comments
 
LVL 7

Expert Comment

by:DanSo1
ID: 23732355
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
 

Author Comment

by:pessi
ID: 23732391
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
 
LVL 7

Expert Comment

by:DanSo1
ID: 23732891
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pessi
ID: 23733113
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
 

Author Comment

by:pessi
ID: 23742423
any idea anyone?
0
 
LVL 7

Expert Comment

by:DanSo1
ID: 23772405
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
 

Author Comment

by:pessi
ID: 23772421
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
 

Author Comment

by:pessi
ID: 23772433
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
 
LVL 7

Accepted Solution

by:
DanSo1 earned 250 total points
ID: 23772443
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
 

Author Comment

by:pessi
ID: 23772456
Daniel

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

Thanks again.
Prasad.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question