ExcelTechnology

Add Word Header and Footer with C#

If a word document has many pages, people may add header or footer to show the page number. Sometimes, the header or footer may be symbols which are the topic of the document talk about or the owner of the document.

It is simple to insert add header and footer. Open a Word document. Then click Header and footer on File menu. Type the text or use the format Word provides.

Then, I want to show something about how to add Word header and footer with C#.

Using the code:

#region add Word header and footer
/**////


/// Add Header
///

/// file name
///
public static bool AddPageHeaderFooter(string filePath)
{

Try

{

Object oMissing = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word._Application WordApp = new Application();

WordApp.Visible = true;

object filename = filePath;

Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,

ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

/**/////Method 1 to Add Header

//WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;

//WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;

//WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "**" );//Header Content

/**/////Method 2 to Add Header
if (WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdNormalView ||
WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdOutlineView)

{

WordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;

}

WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;

WordApp.Selection.HeaderFooter.LinkToPrevious = false;

WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

WordApp.Selection.HeaderFooter.Range.Text = "Header Content";

WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;

WordApp.Selection.HeaderFooter.LinkToPrevious = false;

WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("Footer Content");

//Quit Header and Footer Setting

WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;

//Save file

WordDoc.Save();

WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);

WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);

return true;

}

catch (Exception e)

{

Console.WriteLine(e.Message);

Console.WriteLine(e.StackTrace);

return false;

}
}
#endregion Add Word header and footer

Sometimes, programmers may use components. The components can save programmers much of time to write code and deal with a large amount of documents quickly.

To sum up, in this article, I shows the method to add Word header and footer by using C# and hope it will be helpful for you.

01.06.2011. u 07:27 | 0 Komentara | Print | # | ^

<< Arhiva >>

Creative Commons License
Ovaj blog je ustupljen pod Creative Commons licencom Imenovanje-Dijeli pod istim uvjetima.

< lipanj, 2011 >
P U S Č P S N
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      

Kolovoz 2011 (2)
Srpanj 2011 (2)
Lipanj 2011 (2)
Svibanj 2011 (2)
Travanj 2011 (3)
Ožujak 2011 (2)
Veljača 2011 (2)
Siječanj 2011 (5)
Prosinac 2010 (5)

Dnevnik.hr
Gol.hr
Zadovoljna.hr
Novaplus.hr
NovaTV.hr
DomaTV.hr
Mojamini.tv

Opis bloga

something about Excel Technology in VB.Net and C#

Linkovi

Dnevnik.hr
Video news portal Nove TV

Blog.hr
Blog servis

Igre.hr
Najbolje igre i igrice

Forum.hr
Monitor.hr