View Single Post
 
Old 02-10-2023, 06:49 AM
Ddadoo57 Ddadoo57 is offline Windows 11 Office 2021
Advanced Beginner
 
Join Date: Feb 2023
Posts: 90
Ddadoo57 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
If you want the fields in the footer, then set the range to the footer. The following will put it in all the footer ranges:
Code:
Sub Macro1()
Dim oSection As Section
Dim oFooter As HeaderFooter
Dim oRng As Range
    For Each oSection In ActiveDocument.Sections
        For Each oFooter In oSection.Footers
            If oFooter.Exists Then
                Set oRng = oFooter.Range
                With oRng
                    .Collapse 0
                    .Fields.Add Range:=oRng, Type:=wdFieldIf, Text:="{PAGE} = {NUMPAGES} ""last page""", PreserveFormatting:=False
                    .Collapse 1
                    .MoveEndUntil "}"
                    .End = .End + 1
                    .MoveStartUntil "{"
                    .Text = ""
                    .Fields.Add Range:=oRng, Type:=wdFieldPage, PreserveFormatting:=False
                    .Collapse 0
                    .MoveEndUntil "}"
                    .End = .End + 1
                    .MoveStartUntil "{"
                    .Text = ""
                    .Fields.Add Range:=oRng, Type:=wdFieldNumPages, PreserveFormatting:=False
                    .Fields.Update
                End With
            End If
        Next oFooter
    Next oSection
lbl_Exit:
    Set oSection = Nothing
    Set oFooter = Nothing
    Set oRng = Nothing
    Exit Sub
End Sub
/*******************************************/

Hi Gmayor!

I just have a problem.
The IF field is put only in the .Footers(2) the first page, and the.Footers(1) the odd pages. For the .Footers(3) the even pages, nothing is put in and it crashes on the first .Text = "" with runtime error "6028" impossible to delete the range

I don't have any locked sections in the document.

I have no idea where this can come from?

Last edited by Ddadoo57; 02-10-2023 at 09:56 AM.
Reply With Quote