Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# : Add Footer to Last Page of Word Document

Status
Not open for further replies.

notrut

Programmer
Feb 6, 2002
87
0
0
CA
Hi,

The code below creates the following equation in all the footers in a Word Document:

IF {PAGE} = {PAGENUM} "{AUTHOR}"

Which displays the following in the footer:

Page one: IF 1 = 2 "Author"
Page two: IF 2 = 2 "Author"

I would like this equation to be in it's own field so that "Author" is only displayed on the last page.

This works when I highlight that equation and press Ctrl F9 (i.e. Displays Author on the last page.)

Is there a way to do the Ctrl F9 in Code .... make that equation so that it is it's on field. { IF {PAGE} = {PAGENUM} "{AUTHOR}" }

Any helps would be greatly appreciated.

object collapseDirection = WdCollapseDirection.wdCollapseStart;
object fieldPages = WdFieldType.wdFieldPage;
object fieldNumPages = WdFieldType.wdFieldNumPages;
object fieldMerge = WdFieldType.wdFieldMergeField;
object fieldAuthor = WdFieldType.wdFieldAuthor;

Microsoft.Office.Interop.Word.Range Rng = activeDoc.Sections.Last.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;

object txt = string.Empty;
Rng.Fields.Add(Rng, ref fieldAuthor, ref txt, ref preserveFormatting);
Rng.InsertAfter("\"");
Rng.InsertBefore("\"");
Rng.Collapse(ref collapseDirection);

activeDoc.Fields.Add(Rng, ref fieldNumPages, ref txt, ref preserveFormatting);
Rng.InsertBefore(" = ");
Rng.Collapse(ref collapseDirection);

activeDoc.Fields.Add(Rng, ref fieldPages, ref txt, ref preserveFormatting);
Rng.InsertBefore(" IF ");
Rng.Collapse(ref collapseDirection);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top