Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 11-29-2024, 10:52 AM
gmaxey gmaxey is offline VBA Insert tab before array of words Windows 10 VBA Insert tab before array of words Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,636
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Shelley Lou, Vivka


While a "Find and Replace" solution might be defined for this problem, I think it would be fairly complex. I sense that the amount of text to check is not in the range of say War and Peace so this method may work:

Code:
Sub InsertTab_Before_FirstInstance()
Dim arrWords
Dim oCol As New Collection
Dim lngIndex As Long
Dim lngPar As Long
Dim oWord As Range
   'Define the words to trigger first instance.
   arrWords = Split("means|includes|any|has", "|")
   'Add first instance words to a keyed collection. Note-once added, the same word can't be added again.
   For lngIndex = 0 To UBound(arrWords)
     oCol.Add arrWords(lngIndex), arrWords(lngIndex)
   Next lngIndex
   'Look at each paragraph
   For lngPar = 1 To ActiveDocument.Paragraphs.Count
     'Look at each word starting with first word
     For Each oWord In ActiveDocument.Paragraphs(lngPar).Range.Words
       On Error Resume Next
       'Attempt to add word to collection
       oCol.Add Trim(oWord), Trim(oWord)
       If Err.Number = 0 Then
         'If you can add it, there will be no error.  Remove the word
         oCol.Remove oCol.Count
       Else
         'If you can't then it is a first instance word. Prefix the tab
         If oWord.Characters.First.Previous = " " Then oWord.Characters.First.Previous.Delete
         oWord.InsertBefore Chr(9)
         Err.Clear
         GoTo Next_Par
       End If
      Next oWord
Next_Par:
   Next lngPar
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
 

Thread Tools
Display Modes


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 09:46 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