Thursday, March 12, 2009

C# Word Insert Page numbers in Footer

To use this code you need to add reference to the word object. See this post.
To insert page numbers automatically to your word document is super easy.
The steps are always.

1. Open the word document
2. Insert page number (in footer)
3. Save the document to a new location
4. quit application.

Here is the code to do that.

string inputFilePath = @"C:\Documents and Settings\prgu\My Documents\myinputfile.doc";
string outPutFilePath = @"C:\Documents and Settings\prgu\My Documents\myoutputfile.doc";

object Missing = System.Reflection.Missing.Value;
object fileToOpen = (object)@inputFilePath;
object fileToSave = (object)@outPutFilePath;
Word.Application app = new Word.ApplicationClass();
Word.Document doc = new Word.Document();

//set this to true, else it will actually open the word file in MS word
object myreadonly = true;

//open the word document
doc = app.Documents.Open(ref fileToOpen,
ref Missing, ref myreadonly, 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);

//Insert Page numbers
Word.Window activeWindow = doc.Application.ActiveWindow;
//define a currentpage object
object currentPage = Word.WdFieldType.wdFieldPage;
activeWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter;
//set alignment
activeWindow.ActivePane.Selection.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
//Add page number
activeWindow.Selection.Fields.Add(
activeWindow.Selection.Range, ref currentPage, ref Missing, ref Missing);

// Go back to the Main Document view
activeWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;

//Finally we will save and close the doc
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);

app.NormalTemplate.Saved = true;
doc.Close(ref Missing, ref Missing, ref Missing);
app.Quit(ref Missing, ref Missing, ref Missing);

C# Word (doc file) Insert Text and date into Header.

We will look at how to open a word file in your winforms application and insert a text into the header. Before we go into the code, there are some references you need to import into your application. Lets see how to do that in visual studio.

1. Right click on the solution or the "references" folder and choose "add reference".
2. In the add reference window, choose the "Com" tab and select "Microsoft Word 11.0 Object Library" and click ok.


3. In your code behind Form1.cs add the following namespace.

using Word = Microsoft.Office.Interop.Word;

4. Now your application is configured properly.

Lets look at the code that inserts a text into the header of the word file.
It is well commented to make life easier.

string inputFilePath = @"C:\Documents and Settings\prasad\My Documents\myinputfile.doc";
string outPutFilePath = @"C:\Documents and Settings\prasad\My Documents\myoutputfile.doc";

object Missing = System.Reflection.Missing.Value;
object fileToOpen = (object)@inputFilePath;
object fileToSave = (object)@outPutFilePath;
Word.Application app = new Word.ApplicationClass();
Word.Document doc = new Word.Document();

//set this to true, else it will actually open the word file in MS word
object myreadonly = true;

//open the word document
doc = app.Documents.Open(ref fileToOpen,
ref Missing, ref myreadonly, 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);

// Get todays date and convert it to string
DateTime mydate = DateTime.Now;
mydate.ToString();

//Now the code to insert the header is
foreach (Word.Section wordSection in doc.Sections)
wordSection.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "My Header Text" + " Today is " + mydate;

//Finally we will save and close the doc
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);

app.NormalTemplate.Saved = true;
doc.Close(ref Missing, ref Missing, ref Missing);
app.Quit(ref Missing, ref Missing, ref Missing);


Hope that helps someone.