I wish to be able to run a VBA module which manipulates the table that I'm currently in (i.e., the cursor is somewhere within that table). The VBA code will perform an identical operation on each table that you're in when you run it.
So, for example, let's say I have a module which needed to bold the top row of each table (the headings). It would need to locate the table object (called whatever
) that you're currently in so that it could manipulate whatever.rows(0)
.
How can I get the table object from the cursor position? I also need to detect if I'm not in a table and do nothing (or raise an error dialog).
For a basic table, click Insert > Table and move the cursor over the grid until you highlight the number of columns and rows you want. For a larger table, or to customize a table, select Insert > Table > Insert Table. Tips: If you already have text separated by tabs, you can quickly convert it to a table.
A Table object represents a Word table. The Tables collection contains all of the tables in a range, selection, or document and is returned by the Tables property of the Range, Selection, or Document object. The properties and methods of a Table object are: Application. Creator.
The VBA subroutine at the bottom of this answer shows how to do this.
It uses the current selection, collapsing it to the starting point first so as to not have to worry about multi-segment selections:
Selection.Collapse Direction:=wdCollapseStart
It then checks that selection to ensure it's inside a table
If Not Selection.Information(wdWithInTable) Then
MsgBox "Can only run this within a table"
Exit Sub
End If
The table is then accessible by referring to Selection.Tables(1)
.
The code below was a simple proof of concept which simply toggled each of the starting cells in each row of the table to either insert or delete a vertical bar marker.
Sub VertBar()
' Collapse the range to start so as to not have to deal with '
' multi-segment ranges. Then check to make sure cursor is '
' within a table. '
Selection.Collapse Direction:=wdCollapseStart
If Not Selection.Information(wdWithInTable) Then
MsgBox "Can only run this within a table"
Exit Sub
End If
' Process every row in the current table. '
Dim row As Integer
Dim rng As Range
For row = 1 To Selection.Tables(1).Rows.Count
' Get the range for the leftmost cell. '
Set rng = Selection.Tables(1).Rows(row).Cells(1).Range
' For each, toggle text in leftmost cell. '
If Left(rng.Text, 2) = "| " Then
' Change range to first two characters and delete them. '
rng.Collapse Direction:=wdCollapseStart
rng.MoveEnd Unit:=wdCharacter, Count:=2
rng.Delete
Else
' Just insert the vertical bar. '
rng.InsertBefore ("| ")
End If
Next
End Sub
I realise this is a rather old question, but I stumbled across some code that may help the next person who is facing a similar problem.
ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.count
This will return the index of the table the cursor is in. Which can then be used to make changes or retrieve information:
dim numberOfColumnsInCurrentTable as Integer
dim currentTableIndex as Integer
currentTableIndex = ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.count
numberOfColumns = ActiveDocument.Tables(currentTableIndex).Columns.count
Obviously checks should be added to ensure the cursor is within a table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With