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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programmatically Add Footer to Last Page

Status
Not open for further replies.

notrut

Programmer
Feb 6, 2002
87
CA
Hi,

I would like to add a footer programmatically (C#) to the last page on word documents.

I can insert the footer on a last page, by creating a field that looks like the following:

{ IF { PAGE } = { NUMPAGES } "my Last Page Text goes here" }

However, I want to include fields within the field like this:

{ IF { PAGE } = { NUMPAGES } "my Last Page Text goes here Page {PAGE} of {NUMPAGES} " }

I am having problems create a field of fields.....

Any ideas would be greatly appreciated.
 
Unless you insert a Section break before the last page, you cannot do this without fields. What is the problem with creating the fields? In VBA, you could use code like:
Code:
Sub InsertConditionalPageField()
Application.ScreenUpdating = False
ActiveWindow.View.ShowFieldCodes = True
Dim Rng As Range, RngFld As Range
With ActiveDocument
  Set Rng = .Sections.Last.Headers(wdHeaderFooterPrimary).Range
  .Fields.Add Range:=Rng, Type:=wdFieldEmpty, PreserveFormatting:=False, _
    Text:="= - \# " & Chr(34) & ";;'Last Page Text goes here'" & Chr(34)
  Set RngFld = Rng.Characters(6)
  .Fields.Add Range:=RngFld, Type:=wdFieldNumPages, PreserveFormatting:=False
  Set RngFld = Rng.Characters(4)
  .Fields.Add Range:=RngFld, Type:=wdFieldPage, PreserveFormatting:=False
  Rng.Fields.Update
ActiveWindow.View.ShowFieldCodes = False
End With
Application.ScreenUpdating = True
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
Thanks Paul. This does work using VBA, however, it does not work using C#. Basically the RNG.Characters(6) is not valid.

Here' what I'm trying to do, however, it won't nest:

Microsoft.Office.Interop.Word.Range Rng = activeDoc.Sections.Last.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
object txt = "Outside Range";
activeDoc.Fields.Add(Rng, ref fieldEmpty, ref txt, ref preserveFormatting);

Microsoft.Office.Interop.Word.Range newRng = Rng;
object txt2 = "Inside Range ";
Rng.Fields.Add(newRng, ref fieldEmpty, ref txt2, ref preserveFormatting);

Any help would be greatly appreciated!!
 
Hi,

I have revised my code to what is listed below. This creates the following equation in all the footers:

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

Which displays the following:

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

When I highlight that equation and press Ctrl F9, then the footer behaves the way I would like it. 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);
 
Again, the revised field code is quite straightforward in VBA:
Code:
Sub InsertConditionalPageField()
Application.ScreenUpdating = False
ActiveWindow.View.ShowFieldCodes = True
Dim Rng As Range, RngFld As Range
With ActiveDocument
  Set Rng = .Sections.Last.Headers(wdHeaderFooterPrimary).Range
  .Fields.Add Range:=Rng, Type:=wdFieldEmpty, PreserveFormatting:=False, Text:="IF =  "
  Set RngFld = Rng.Characters(9)
  .Fields.Add Range:=RngFld, Type:=wdFieldAuthor, PreserveFormatting:=False
  Set RngFld = Rng.Characters(7)
  .Fields.Add Range:=RngFld, Type:=wdFieldNumPages, PreserveFormatting:=False
  Set RngFld = Rng.Characters(5)
  .Fields.Add Range:=RngFld, Type:=wdFieldPage, PreserveFormatting:=False
  Rng.Fields.Update
ActiveWindow.View.ShowFieldCodes = False
End With
Application.ScreenUpdating = True
End Sub
Granted, C# is a different programming language, but you should be able to achieve the same result. I suppose the fundamental issue, though, is why you're doing this at all in code. You should be using a template that already has all of the required features in place.

As for your own code:
1. I note you're using 'ref preserveFormatting'. All adding preserveFormatting does is add bloat to the field codes. Use False or Empty instead.
2. Why do you have 'object fieldMerge = WdFieldType.wdFieldMergeField'? You're not apparently working with mergefields.
3. It's not apparent what the following two lines are meant to achieve, other than adding '\' characters before & after the field:
Rng.InsertAfter("\"");
Rng.InsertBefore("\"");

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top