Trình con VBA ở cuối câu trả lời này cho thấy cách thực hiện việc này.
Nó sử dụng các lựa chọn hiện tại, sụp đổ nó vào điểm xuất phát đầu tiên để không phải lo lắng về các lựa chọn đa phân khúc:
Selection.Collapse Direction:=wdCollapseStart
Sau đó nó sẽ kiểm tra rằng lựa chọn để đảm bảo nó là bên trong một bảng
If Not Selection.Information(wdWithInTable) Then
MsgBox "Can only run this within a table"
Exit Sub
End If
Bảng sau đó có thể truy cập được bằng cách tham chiếu đến Selection.Tables(1)
.
Mã chứng minh đơn giản về khái niệm chỉ đơn giản là bật mỗi ô khởi động trong mỗi hàng của bảng để chèn hoặc xóa một vạch đánh dấu dọc.
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
Hoàn hảo! Đây chính xác là những gì tôi cần @enifeder: ActiveDocument.Range (0, Selection.Tables (1) .Range.End) .Tables.count – DRC