La subrutina de VBA en la parte inferior de esta respuesta muestra cómo hacerlo.
Se utiliza la selección actual, colapsando hasta el punto de partida en primer lugar con el fin de no tener que preocuparse de selecciones múltiples segmentos:
Selection.Collapse Direction:=wdCollapseStart
A continuación, comprueba que la selección para asegurarse de que está dentro de una tabla
If Not Selection.Information(wdWithInTable) Then
MsgBox "Can only run this within a table"
Exit Sub
End If
A continuación, se puede acceder a la tabla consultando Selection.Tables(1)
.
El código de abajo era una simple prueba de concepto que simplemente alternar cada una de las células de partida en cada fila de la tabla para insertar o eliminar un marcador barra vertical.
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
¡Perfecto! Esto es exactamente lo que necesitaba @enifeder: ActiveDocument.Range (0, Selection.Tables (1) .Range.End) .Tables.count – DRC