View Single Post
 
Old 11-30-2024, 07:42 AM
gmaxey gmaxey is offline Windows 10 Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Vivka,


Thanks. I didn't have time yesterday to delve into a FR solution. Here is one that seems to work. I'm not confident it is any better than the solution posted yesterday:

Code:
Sub InsertTab_Before_FirstInstance()
Dim oPar As Paragraph
Dim oRng As Word.Range, oFIRng As Range
Dim arrWords
Dim i As Long, j As Long
  arrWords = Array("means", "includes", "has", "any")
  For Each oPar In ActiveDocument.Range.Paragraphs
    Set oFIRng = Nothing
    Set oRng = oPar.Range
    j = 0
    Do
      For i = j To UBound(arrWords)
        oRng.Start = oPar.Range.Start
        With oRng.Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = arrWords(i)
          .MatchWholeWord = True
          If .Execute Then
            'We have found a first instance word.  But, does another FI word precede it?
            Set oFIRng = oRng.Duplicate
          End If
          'Index and look for next word in the array
          j = j + 1
          If j = UBound(arrWords) + 1 Then Exit Do
        End With
      Next i
      If oFIRng Is Nothing Then Exit Do
    Loop
    If Not oFIRng Is Nothing Then
      If oFIRng.Characters.First.Previous = " " Then oFIRng.Characters.First.Previous.Delete
      If Not oFIRng.Characters.First.Previous = Chr(9) Then oFIRng.InsertBefore Chr(9)
    End If
  Next
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote