Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you get the current table in MS Word VBA?

Tags:

ms-word

vba

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).

like image 606
paxdiablo Avatar asked Aug 29 '11 06:08

paxdiablo


People also ask

How do you get a table on Word?

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.

What is the table object function in Word?

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.


2 Answers

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
like image 167
paxdiablo Avatar answered Sep 21 '22 02:09

paxdiablo


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.

like image 38
enifeder Avatar answered Sep 17 '22 02:09

enifeder