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

Re: How to get the "next table" and the "previous table" by Jay

Jay
Thu Jan 06 22:53:28 CST 2005

On Fri, 7 Jan 2005 10:16:07 +0800, "animator" <lidelu@sina.com> wrote:

>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

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org