Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 11-29-2024, 07:25 AM
Shelley Lou Shelley Lou is offline VBA Insert tab before array of words Windows 10 VBA Insert tab before array of words Office 2016
Expert
VBA Insert tab before array of words
 
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
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Insert tab before array of words VBA help non breaking spaces for array of words not working correctly Shelley Lou Word VBA 2 03-08-2023 03:15 AM
VBA Insert tab before array of words Macro to insert certain words if the number of words than 20 laith93 Word VBA 6 10-28-2022 01:12 AM
VBA Insert tab before array of words Insert words before and after any italics benfarley Word VBA 1 03-30-2022 08:35 PM
An array of words from a document knowing the font style Kreol2013 Word VBA 0 07-08-2013 01:29 AM
VBA Insert tab before array of words Convert String Array to Integer Array from a User Input? tinfanide Excel Programming 4 12-26-2012 08:56 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:26 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft