views:

103

answers:

2

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)?

+3  A: 

I just happened to be working on code where I already do this in Excel from C#... This is partial, and will get you started...

Microsoft.Office.Interop.Excel.Application excelapp = new Microsoft.Office.Interop.Excel.Application();
excelapp.Visible = true;
Microsoft.Office.Interop.Excel._Workbook book = (Microsoft.Office.Interop.Excel._Workbook)excelapp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); ;
Microsoft.Office.Interop.Excel._Worksheet sheet = (Microsoft.Office.Interop.Excel._Worksheet)book.ActiveSheet;


sheet.get_Range("A1", "N999").Font.Size = "8";
sheet.PageSetup.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperLegal;
sheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
sheet.PageSetup.PrintTitleRows = "$3:$5";
sheet.PageSetup.PrintTitleColumns = "$A:$B";

There's more code there than you need for this specific task, but the relevant lines for having a header (or something that repeats at the top of each page) are:

sheet.PageSetup.PrintTitleRows = "$3:$5";
sheet.PageSetup.PrintTitleColumns = "$A:$B";

Edit - Added

Here's a link to MSDN documentation, for all your Office Interop needs.

http://msdn.microsoft.com/en-us/library/bb209015(office.12).aspx

David Stratton
+4  A: 

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;
}
Bharath K
Cool! I didn't know how to do it in Word, but that's because I've never had to. I'm bookmarking this page for your answer, because I know I'll need it later. +1.
David Stratton