Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I figure out what table number I am in?

Status
Not open for further replies.

pat2e69

Technical User
Feb 13, 2006
2
US
How do I figure out what table number I am in in a Word document? I need to write a macro that will right align numbers by their right-most digit excluding any trailers (%, x, or parens) but I first need to get the table number so that I can do a row and column count.

Sincerely,
Pat2e69
 
If you are being literal, and "being in" means where the Selection (cursor) is, then if the selection is actually in a table, the number is always 1. You may want to check to se if the selection is actially in a table.
Code:
Sub tablecount()
Dim rowNum As Integer
Dim colNum As Integer
If Selection.Information(wdWithInTable) = True Then
   rowNum = Selection.Tables(1).Rows.Count
   colNum = Selection.Tables(1).Columns.Count
   MsgBox rowNum & "   " & colNum
Else
   MsgBox "Selection is not in a table."
End If
End Sub
On the other hand, to go to a specific table can be somewhat tricky. To go to the third table in a document - so the Selection is IN it, go the the beginning of the document, then move to the third table.
Code:
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=3
Or, to move forward a specific number of tables (say 2) - from the current Selection point
Code:
Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=2


Gerry
 
Thank you fumei,

I'm an idiot. I had the Selection.Cells(1).ColumnIndex correct to find the Column Number I was in, But for the count I used:
ActiveDocument.Tables(1).Columns.Count and it was always counting the columns for the topmost table in the document.

I had a feeling that this answer would be simple, now comes the harder part of figuring out how to align by the right-most numerical digit. Any ideas?

Thanks again
Pat2e69
 
Hmmmm, have you tried the macro recorder?

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top