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.
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.
ASKER
Daniel
Thank you for the answer. I did that...
and got this..
Sub Macro1()
Selection.Sections(1).Foot ers(1).Pag eNumbers.A dd PageNumberAlignment:= _
wdAlignPageNumberRight, FirstPage:=True
End Sub
I dont know any VB can you please help me give a c# equivalent?
Thanks
Prasad
Thank you for the answer. I did that...
and got this..
Sub Macro1()
Selection.Sections(1).Foot
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.
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
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
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
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
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.
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.
ASKER
here was the solution.
//SETTING THE FOCUES ON THE PAGE FOOTER
oWord.ActiveWindow.ActiveP ane.View.S eekView = Word.WdSeekView.wdSeekCurr entPageFoo ter;
//ENTERING A PARAGRAPH BREAK "ENTER"
oWord.Selection.TypeParagr aph();
String docNumber = "1";
String revisionNumber = "0";
//INSERTING THE PAGE NUMBERS CENTRALLY ALIGNED IN THE PAGE FOOTER
oWord.Selection.Paragraphs .Alignment = Word.WdParagraphAlignment. wdAlignPar agraphLeft ;
oWord.ActiveWindow.Selecti on.Font.Na me = "Arial";
oWord.ActiveWindow.Selecti on.Font.Si ze = 8;
oWord.ActiveWindow.Selecti on.TypeTex t("Documen t #: " + docNumber + " - Revision #: " + revisionNumber);
//INSERTING TAB CHARACTERS
oWord.ActiveWindow.Selecti on.TypeTex t("\t");
oWord.ActiveWindow.Selecti on.TypeTex t("\t");
oWord.ActiveWindow.Selecti on.TypeTex t("Page ");
Object CurrentPage = Word.WdFieldType.wdFieldPa ge;
oWord.ActiveWindow.Selecti on.Fields. Add(oWord. Selection. Range, ref CurrentPage, ref oMissing, ref oMissing);
oWord.ActiveWindow.Selecti on.TypeTex t(" of ");
Object TotalPages = Word.WdFieldType.wdFieldNu mPages;
oWord.ActiveWindow.Selecti on.Fields. Add(oWord. Selection. Range, ref TotalPages, ref oMissing, ref oMissing);
//SETTING FOCUES BACK TO DOCUMENT
oWord.ActiveWindow.ActiveP ane.View.S eekView = Word.WdSeekView.wdSeekMain Document;
//SETTING THE FOCUES ON THE PAGE FOOTER
oWord.ActiveWindow.ActiveP
//ENTERING A PARAGRAPH BREAK "ENTER"
oWord.Selection.TypeParagr
String docNumber = "1";
String revisionNumber = "0";
//INSERTING THE PAGE NUMBERS CENTRALLY ALIGNED IN THE PAGE FOOTER
oWord.Selection.Paragraphs
oWord.ActiveWindow.Selecti
oWord.ActiveWindow.Selecti
oWord.ActiveWindow.Selecti
//INSERTING TAB CHARACTERS
oWord.ActiveWindow.Selecti
oWord.ActiveWindow.Selecti
oWord.ActiveWindow.Selecti
Object CurrentPage = Word.WdFieldType.wdFieldPa
oWord.ActiveWindow.Selecti
oWord.ActiveWindow.Selecti
Object TotalPages = Word.WdFieldType.wdFieldNu
oWord.ActiveWindow.Selecti
//SETTING FOCUES BACK TO DOCUMENT
oWord.ActiveWindow.ActiveP
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Daniel
Sorry if I offended you. My intention wasnt that.
I heartfully thank you for trying to help me out.
Thanks again.
Prasad.
Sorry if I offended you. My intention wasnt that.
I heartfully thank you for trying to help me out.
Thanks again.
Prasad.
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