Doug

Doug

  • NA
  • 1
  • 0

Creating Word document programmatically with different Footers.

Oct 8 2008 3:17 PM

Using Visual Studio/C# 2008

I am generating a report with multiple pages, the first 3 pages need different footers.  I have successfully created a footer on the title page, and the TOC (Table of Contents) or second page.  However, when I try to add a different footer to the 3rd page it adds the same text and fields to the second page footer as well.  Below is a snippet of my code thus far.  I have commented out the last footer section since it doesn't work.  The only difference between page2 footer and page3 footer is the format of the page number.  Can anyone shed some light on this please?  I have been banging my head against a wall for a few hours now and it is starting to hurt.

 

using Microsoft.Office.Interop.Word;

using Microsoft.Office.Core;

 

Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();

Document varDoc = varWord.Documents.Add(ref varMissing, ref varMissing, ref varMissing, ref varTrueValue);

varDoc.Activate();

//First Page Header

varDoc.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;

varDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;

varDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderBottom].LineWidth = WdLineWidth.wdLineWidth300pt;

varDoc.ActiveWindow.Selection.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

varDoc.ActiveWindow.Selection.Font.Name = "Times New Roman";

varDoc.ActiveWindow.Selection.Font.Size = 16;

varDoc.ActiveWindow.Selection.Font.Bold = 1;

varDoc.ActiveWindow.Selection.TypeText("FOR OFFICIAL USE ONLY");

//Header TOC

varDoc.ActiveWindow.ActivePane.View.NextHeaderFooter();

varDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;

varDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderBottom].LineWidth = WdLineWidth.wdLineWidth300pt;

varDoc.ActiveWindow.Selection.Font.Name = "Times New Roman";

varDoc.ActiveWindow.Selection.Font.Size = 11;

varDoc.ActiveWindow.Selection.TypeText(proj.projectAcronym + "\t\tAppendix P Test and Evaluation Report");

varDoc.ActiveWindow.Selection.TypeParagraph();

Object currentMonthYear = "MMMM yyyy";

varDoc.ActiveWindow.Selection.InsertDateTime(ref currentMonthYear, ref varTrueValue, ref varMissing, ref varMissing, ref varMissing);

varDoc.ActiveWindow.Selection.TypeText("\t\tVersion X.x");

varDoc.ActiveWindow.Selection.HeaderFooter.LinkToPrevious = false;

//First Page Footer

varDoc.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;

varDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;

varDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderTop].LineWidth = WdLineWidth.wdLineWidth300pt;

varDoc.ActiveWindow.Selection.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

varDoc.ActiveWindow.Selection.Font.Name = "Times New Roman";

varDoc.ActiveWindow.Selection.Font.Size = 16;

varDoc.ActiveWindow.Selection.Font.Bold = 1;

varDoc.ActiveWindow.Selection.TypeText("FOR OFFICIAL USE ONLY");

//Footer TOC

varDoc.ActiveWindow.ActivePane.View.NextHeaderFooter();

varDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;

varDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderTop].LineWidth = WdLineWidth.wdLineWidth300pt;

varDoc.ActiveWindow.Selection.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

varDoc.ActiveWindow.Selection.Font.Name = "Times New Roman";

varDoc.ActiveWindow.Selection.Font.Size = 16;

varDoc.ActiveWindow.Selection.Font.Bold = 1;

varDoc.ActiveWindow.Selection.TypeText("FOR OFFICIAL USE ONLY");

varDoc.ActiveWindow.Selection.TypeParagraph();

varDoc.ActiveWindow.Selection.Font.Size = 11;

varDoc.ActiveWindow.Selection.Font.Bold = 0;

varDoc.ActiveWindow.Selection.TypeText("P-");

object CurrentPage = WdFieldType.wdFieldPage;

varDoc.ActiveWindow.Selection.Fields.Add(varDoc.ActiveWindow.Selection.Range, ref CurrentPage, ref varMissing, ref varMissing);

varDoc.ActiveWindow.Selection.HeaderFooter.LinkToPrevious = false;

varDoc.ActiveWindow.Selection.HeaderFooter.PageNumbers.NumberStyle = WdPageNumberStyle.wdPageNumberStyleLowercaseRoman;

varDoc.ActiveWindow.Selection.HeaderFooter.PageNumbers.RestartNumberingAtSection = true;

//Footer Rest of Doc

//varDoc.ActiveWindow.ActivePane.View.NextHeaderFooter();

//varDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;

//varDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderTop].LineWidth = WdLineWidth.wdLineWidth300pt;

//varDoc.ActiveWindow.Selection.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

//varDoc.ActiveWindow.Selection.Font.Name = "Times New Roman";

//varDoc.ActiveWindow.Selection.Font.Size = 16;

//varDoc.ActiveWindow.Selection.Font.Bold = 1;

//varDoc.ActiveWindow.Selection.TypeText("FOR OFFICIAL USE ONLY");

//varDoc.ActiveWindow.Selection.TypeParagraph();

//varDoc.ActiveWindow.Selection.Font.Size = 11;

//varDoc.ActiveWindow.Selection.Font.Bold = 0;

//varDoc.ActiveWindow.Selection.TypeText("P-");

//CurrentPage = WdFieldType.wdFieldPage;

//varDoc.ActiveWindow.Selection.Fields.Add(varDoc.ActiveWindow.Selection.Range, ref CurrentPage, ref varMissing, ref varMissing);

//varDoc.ActiveWindow.Selection.HeaderFooter.LinkToPrevious = false;

//varDoc.ActiveWindow.Selection.HeaderFooter.PageNumbers.NumberStyle = WdPageNumberStyle.wdPageNumberStyleArabic;

//varDoc.ActiveWindow.Selection.HeaderFooter.PageNumbers.RestartNumberingAtSection = true;

//varDoc.ActiveWindow.ActivePane.View.NextHeaderFooter();

varDoc.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;


Answers (1)