INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

C# : Add Footer to Last Page of Word Document

C# : Add Footer to Last Page of Word Document

(OP)
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);

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close