TimLitle
asked on
VBA to insert a modified Page x of y in a Word Footer
I can insert "Page x of y" where "x" is wdFieldPage and "y" is wdFieldNumPages into a Word Footer resulting in:
"Page { PAGE } of { NUMPAGES }"
using the following code:
Sub InsertFooter()
Dim rng As Range
With ThisDocument.Sections(1)
With .Footers(wdHeaderFooterPri mary)
Set rng = .Range.Duplicate
rng.Collapse wdCollapseEnd
rng.InsertBefore vbTab & "Page of "
rng.Collapse wdCollapseStart
rng.Move wdCharacter, 6
ThisDocument.Fields.Add rng, wdFieldPage
Set rng = .Range.Duplicate
rng.Collapse wdCollapseEnd
ThisDocument.Fields.Add rng, wdFieldNumPages
End With
End With
End Sub
How do I change this code to get:
"Page { = { PAGE } -1 } of { = { NUMPAGES } -1 }"
? ? ?
"Page { PAGE } of { NUMPAGES }"
using the following code:
Sub InsertFooter()
Dim rng As Range
With ThisDocument.Sections(1)
With .Footers(wdHeaderFooterPri
Set rng = .Range.Duplicate
rng.Collapse wdCollapseEnd
rng.InsertBefore vbTab & "Page of "
rng.Collapse wdCollapseStart
rng.Move wdCharacter, 6
ThisDocument.Fields.Add rng, wdFieldPage
Set rng = .Range.Duplicate
rng.Collapse wdCollapseEnd
ThisDocument.Fields.Add rng, wdFieldNumPages
End With
End With
End Sub
How do I change this code to get:
"Page { = { PAGE } -1 } of { = { NUMPAGES } -1 }"
? ? ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I still can't figure out how to prevent this blowing up on line 36. I tried all kinds of things including lots of DoEvents and OnError statements although I have not tried running the code when the document is in Draft View instead of Print Layout View.
I eventually settled on code to achieve the following code in a footer containing:
"Page xxx of yyy"
where both xxx and yyy need to be the actual page numbers adjusted by the DocVariable "Pages Adjustment" so:
xxx becomes { = PAGE } + { DOCVARIABLE "Pages Adjustment" } } and
yyy becomes { = NUMPAGES } + { DOCVARIABLE "Pages Adjustment" }
The code that does this for both Odd and Even Footer pages is shown in the code snippet area:
I eventually settled on code to achieve the following code in a footer containing:
"Page xxx of yyy"
where both xxx and yyy need to be the actual page numbers adjusted by the DocVariable "Pages Adjustment" so:
xxx becomes { = PAGE } + { DOCVARIABLE "Pages Adjustment" } } and
yyy becomes { = NUMPAGES } + { DOCVARIABLE "Pages Adjustment" }
The code that does this for both Odd and Even Footer pages is shown in the code snippet area:
For i = 1 To 2
docFooterFile.Sections(1).Footers(IIf(i Mod 2 = 0, wdHeaderFooterPrimary, wdHeaderFooterEvenPages)).Range.Select
With Selection
.Find.Execute findtext:="xxx", replacewith:=""
.Fields.Add Range:=.Range, Type:=wdFieldEmpty, PreserveFormatting:=False
.TypeText Text:="= "
.Fields.Add Range:=.Range, Type:=wdFieldPage, PreserveFormatting:=False
.TypeText Text:=" + "
.Fields.Add Range:=.Range, Type:=wdFieldDocVariable, Text:="""Pages Adjustment""", PreserveFormatting:=False
.Find.Execute findtext:="yyy", replacewith:=""
.Fields.Add Range:=.Range, Type:=wdFieldEmpty, PreserveFormatting:=False
.TypeText Text:="= "
.Fields.Add Range:=.Range, Type:=wdFieldNumPages, PreserveFormatting:=False
.TypeText Text:=" + "
.Fields.Add Range:=.Range, Type:=wdFieldDocVariable, Text:="""Pages Adjustment""", PreserveFormatting:=False
End With
Next i
ASKER
Oops, the following is correct:
xxx becomes { = { PAGE } + { DOCVARIABLE "Pages Adjustment" } } and
yyy becomes { = { NUMPAGES } + { DOCVARIABLE "Pages Adjustment" } }
xxx becomes { = { PAGE } + { DOCVARIABLE "Pages Adjustment" } } and
yyy becomes { = { NUMPAGES } + { DOCVARIABLE "Pages Adjustment" } }
ASKER
I would still be interesting in knowing how to prevent your solution from blowing ip on line 36. Can you re-create it?
Thanks Tim.
I was able to reproduce the problem with certain configurations of data. I have spent some time trying to rework it, but things got more and more complicated as each loophole was plugged. A complete rethink is probably necessary.
I was able to reproduce the problem with certain configurations of data. I have spent some time trying to rework it, but things got more and more complicated as each loophole was plugged. A complete rethink is probably necessary.
The original code by GrahamSkan works perfectly for me. This is a very good solution, just what aI was looking for.
^^ The original code by GrahamSkan works perfectly for me. This is a very good solution, just what I was looking for.
ASKER