View Single Post
 
Old 11-29-2024, 07:25 AM
Shelley Lou Shelley Lou is offline Windows 10 Office 2016
Expert
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Insert tab before array of words

I've come across an issue with the code I use to format a set of definitions into our house style. At the moment the code I use (not the below code) only looks for the the word 'means' and inserts a tab before it but Definition wording often has other common words e.g. 'incudes', 'has the meaning' or 'any' so when I run the code these remain unchanged.

Would the best approach be to put these words into an array to look for the first instance in each paragraph and insert a tab before them. I'm trying to get the code below to work. Am I on the right track or is there a simpler way to achieve what I need the code to do?

Before
Before.JPG

After
After.JPG

Before - Insert Tab.docx


Code:
Sub InsertTab_Before_FirstInstance()
    Dim oRng As Word.Range
    Dim arrWords
    Dim i As Long
    arrWords = Array("means", "includes", "has", "any")
    For i = 0 To UBound(arrWords)
    Set oRng = ActiveDocument.Range
    With oRng.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .text = arrWords(i)
      .MatchWholeWord = True
      .Replacement.text = ChrW(9) & arrWords(i)
      .Execute Replace:=wdReplaceOne
      End With
    Next
End Sub
Reply With Quote