How to get the "next table" and the "previous table"

A

animator

There are some tables in my document, and the cusor focus in somewhere.
How can I get the object of next table, like this statement:
Set oTab = Selection.Tables(1).Next 'this is pseudocode
 
J

Jay Freedman

There are some tables in my document, and the cusor focus in somewhere.
How can I get the object of next table, like this statement:
Set oTab = Selection.Tables(1).Next 'this is pseudocode

The pseudocode is nice, but the Table object doesn't have a Next
method. Try this instead:

Function NextTable() As Table
Dim oTab As Table
Dim nIndex As Long

Set oTab = Nothing ' not really necessary
If Selection.Information(wdWithInTable) Then
' get index of table containing selection
nIndex = ActiveDocument.Range(0, _
Selection.Tables(1).Range.End).Tables.Count

' get the next table (if any)
If nIndex < ActiveDocument.Tables.Count Then
Set oTab = ActiveDocument.Tables(nIndex + 1)
End If
End If
Set NextTable = oTab
Set oTab = Nothing
End Function

Sub test()
Dim myTab As Table
Set myTab = NextTable()
If Not (myTab Is Nothing) Then
MsgBox myTab.Cell(1, 1).Range.Text
myTab.Cell(1, 1).Range.Select
Selection.Collapse wdCollapseStart
End If
End Sub

The technique for getting the absolute index of the selected table is
from this article:
http://word.mvps.org/FAQs/MacrosVBA/GetIndexNoOfPara.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top