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).
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:
It then checks that selection to ensure it’s inside a table
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.