WORKAROUND
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
NOTE: This sample macro may not work correctly in all circumstances.
In the macro, select the range for which you want to retrieve the number of the last page in the range, and use the
Selection object instead of the
Range object.
If you use the
Selection object, it may be a good idea to store the current selection at the beginning of the macro in a Range variable and to set the selection back to its original value at the end of the code that retrieves the page number.
The following sample macro loops through all tables in a document and displays the number of the last page on which each table is located in a message box. It also shows how to store the selection in a Range variable and restore it at the end of the macro. The macro turns the screen updates off (which is optional) and can be used to hide the movement of the selection in the document when the macro is running:
Sub GetTableEndPage()
Dim tbl As Table
Dim rg As Range
' Turn screen updating off.
Application.ScreenUpdating = False
'Save the selection in a range object
Set rg = Selection.Range
Selection.EndKey Unit:=wdStory
'Repaginate
ActiveDocument.Repaginate
'Toggle nonprinting characters twice
ActiveWindow.ActivePane.View.ShowAll = Not _
ActiveWindow.ActivePane.View.ShowAll
ActiveWindow.ActivePane.View.ShowAll = Not _
ActiveWindow.ActivePane.View.ShowAll
For Each tbl In ActiveDocument.Tables
tbl.Select
Selection.MoveEnd wdCharacter, -1
MsgBox "Table ends on page " & _
Selection.Information(wdActiveEndPageNumber)
Next
'Restore the selection via the saved range object
rg.Select
End Sub