Word VBA: Getting the Current Page Number

Sometimes we need to get the current page number when we make a macro.

Macro Example

The following Word VBA macro displays it in a message box.

Sub GetCurrentPageNumber()

  MsgBox Selection.Information(wdActiveEndPageNumber)

End Sub

Description

[Application.]Selection

  Selection...

The Application.Selection property returns the Selection object that represents the current selection in a window or pane of Microsoft Word.

obj-Word-Application-Selection

The Selection property is a member of the Word.Global class, so we can omit the Application property.

obj-Word-Global-Selection

Selection.Information

  Selection.Information...

The Selection.Information property returns information about the specified selection.

obj-Word-Selection-Information

Const: wdActiveEndPageNumber

  Selection.Information(wdActiveEndPageNumber)

We can get the current page number by setting the const: wdActiveEndPageNumber to the parameter of the Selection.Information property.

obj-Word-WdInformation-wdActiveEndPageNumber

MsgBox function

The code: Selection.Information(wdActiveEndPageNumber) is set to the parameter of a MsgBox function.

  MsgBox Selection.Information(wdActiveEndPageNumber)

So, a message box shows the current page number.

Sub GetCurrentPageNumber()

  MsgBox Selection.Information(wdActiveEndPageNumber)

End Sub

Properties

http://www.relief.jp/itnote/archives/word-vba-get-page-number.php

Apply To

  • Word 2013
  • Word 2010
  • Word 2007