Search notes:

Office Object Model: Word - Field

A field allows to insert dynamic text (such as current page number or date) into a document.

Inserting a page number

The following VBA code tries to demonstrate how a page number is inserted into a word document:
option explicit

sub main() ' {

    selection.pageSetup.orientation = wdOrientLandscape

    with selection
     '
     ' Make a really big font
     '
     .font.size = 120

     .typeText "This is page number "
     .fields.add selection.Range, text := "page"

     .typeParagraph

     .typeText "This is page number "
     .fields.add selection.Range, text := "page"

    end with

end sub ' }
Github repository about-MS-Office-object-model, path: /Word/Field/pageNumber.bas

See also

Word Object Model

Index