Link to home
Start Free TrialLog in
Avatar of TimLitle
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(wdHeaderFooterPrimary)
          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
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TimLitle
TimLitle

ASKER

Your cheat is an approach I hadnt contemplated and I learned some interesting techniques from it.  I can step through the code and it works fine.  However, if I run it with no breaks, it blows up on line 36 every time with Run-time error 5904;  Cannot edit range.  I noticed you have a DoEvents at line 24 (why?) so I put a DoEvents before line 36 which didnt help.  I then added an On Error GoTo to Resume and try the failed instruction again.  This has worked in other situations but not here.  How do I fix this the problem and why dont either the DoEvents or On Error GoTo approaches work?
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:
    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

Open in new window

Oops, the following is correct:

xxx becomes { = { PAGE } + { DOCVARIABLE "Pages Adjustment" } } and
yyy becomes { = { NUMPAGES } + { DOCVARIABLE "Pages Adjustment" } }
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.
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.