Home > OS >  Configure a footer in a word document (page numbers and text)
Configure a footer in a word document (page numbers and text)

Time:03-17

I'm trying to set up a Word document out of Excel what actually works pretty well. However, the configuration of the footer is somehow a riddle.

The footer shall contain a date (String from variable VDate), a vertical line | and a Page X of Y. Here's what I've coded so far:

...
VDate = "31.03.2022"    'String

Set WordRange = objDoc.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range

WordRange.Delete

With WordRange
    .Text = VDate & "  |  Page "
    .Collapse wdCollapseEnd
    .Fields.Add WordRange, wdFieldPage        '<-- works until here
    .Collapse wdCollapseEnd
    .InsertAfter " of "
    .Fields.Add WordRange, wdFieldNumPages
End With
...

After adding the first field wdFieldPage the second Collapse does not work and the word of is inserted directly after Page and the second field wdFieldNumPages comes directly after the first field, leading to this:

"31.03.2022 | Page of 11"

Can anybody help? Many thanks in advance!

CodePudding user response:

Put a .MoveEnd after .Collapse wdCollapseEnd works:

VDate = "31.03.2022"    'String

Set WordRange = objDoc.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range

WordRange.Delete

With WordRange
    .Text = VDate & "  |  Page "
    .Collapse wdCollapseEnd
    .Fields.Add WordRange, wdFieldPage
    .Collapse wdCollapseEnd
    .MoveEnd
    .InsertAfter " of "
    .Collapse wdCollapseEnd
    .MoveEnd
    .Fields.Add WordRange, wdFieldNumPages
End With

enter image description here

  • Related