Thread: Text Insert
View Single Post
 
Old 04-10-2024, 07:11 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Rather than looping through every table, I would use the Find command along the following lines.
Code:
Sub TableTags()
  Dim str As String, aRng As Range, sPrefix() As String, i As Integer
  Dim insertTextAbove() As String, insertTextBelow() As String
  sPrefix = Split("B|E|I", "|")
  insertTextAbove = Split("123|456|789", "|")
  insertTextBelow = Split("333|666|999", "|")
  
  For i = LBound(sPrefix) To UBound(sPrefix)
    Set aRng = ActiveDocument.Range
    With aRng.Find
      .Forward = True
      .Text = sPrefix(i) & "BEGINING"
      If .Execute Then
        If aRng.Information(wdWithInTable) Then
          Set aRng = aRng.Tables(1).Range
          aRng.Collapse Direction:=wdCollapseStart
          aRng.MoveStart Unit:=wdCharacter, Count:=-1
          aRng.InsertBefore vbCrLf & insertTextAbove(i)
          GoTo NextOne
        End If
      End If
    End With
NextOne:
  Next i
  
  For i = LBound(sPrefix) To UBound(sPrefix)
    Set aRng = ActiveDocument.Range
    With aRng.Find
      .Forward = False
      .Text = sPrefix(i) & "ENDING"
      If .Execute Then
        If aRng.Information(wdWithInTable) Then
          Set aRng = aRng.Tables(1).Range
          aRng.Collapse Direction:=wdCollapseEnd
          'aRng.MoveEnd Unit:=wdCharacter, Count:=1
          aRng.InsertAfter insertTextBelow(i) & vbCrLf
          GoTo NextTwo
        End If
      End If
    End With
NextTwo:
  Next i
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote