Ответ 1
Подпрограмма VBA в нижней части этого ответа показывает, как это сделать.
Он использует текущий выбор, сначала сбрасывая его в исходную точку, чтобы не беспокоиться о выборе нескольких сегментов:
Selection.Collapse Direction:=wdCollapseStart
Затем он проверяет этот выбор для обеспечения его внутри таблицы
If Not Selection.Information(wdWithInTable) Then
MsgBox "Can only run this within a table"
Exit Sub
End If
Затем таблица доступна для обращения к Selection.Tables(1)
.
Приведенный ниже код был простым доказательством концепции, которое просто переключило каждую из начальных ячеек в каждой строке таблицы, чтобы либо вставить, либо удалить маркер вертикальной строки.
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