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

守給你的承諾、 提交于 2020-08-22 09:25:48

问题


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


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/7226721/how-can-you-get-the-current-table-in-ms-word-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!