Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add footer to Office Word/Excel document

I'm looking to build a solution similar to this one: http://esqinc.com/section/products/4/idocid.html

What the system makes is insertion of a document file name into the document footer. How's that possible programmatically (preferably in .NET)?

like image 576
SharpAffair Avatar asked Aug 03 '10 15:08

SharpAffair


1 Answers

Hoping this gets you started. The following pseudo c# code can be used to add text to footer. Only you will have to do this in a macro to completely automate this and also identify the document name to be added. Finally call in to the macro during Document Save to add the footer text.

foreach ( Section wordSection in wordDoc.Sections )
{
  HeaderFooter footer = wordSection.Footers[ Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary ];
  footer.Range.Select( );
  footer.Range.Text = footerTxt;
  hf.Range.Font.Size = 10;
  wordApp.Selection.Paragraphs[ 1 ].Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
  wordApp.Selection.Paragraphs[ 1 ].SpaceAfter = 0;
}
like image 107
Bharath K Avatar answered Sep 19 '22 10:09

Bharath K