Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-14-2016, 07:36 AM
dan88 dan88 is offline Find only the first Instance of Words -Then Do Replacement Windows 10 Find only the first Instance of Words -Then Do Replacement Office 2016
Novice
Find only the first Instance of Words -Then Do Replacement
 
Join Date: Feb 2016
Posts: 24
dan88 is on a distinguished road
Default Find only the first Instance of Words -Then Do Replacement

Hello,

I would be grateful for help on my code below.



I just want to find the first instance of each words in the array and then do a replace task on it.

Example find the first instance of Air Boat then do a replacement on that only. Ignore all other Air Boats

Code:
Sub Insert_After_First_Instance()

    Dim oRng As Word.Range
    Dim arrWords
    Dim i As Long
    arrWords = Array("Air Boat", "Power Boat", "Ferry")
    arrInsertAfter = Array("AB123", "PB456", "F123")
  
For i = 0 To UBound(arrWords)
        Set oRng = ActiveDocument.Range
        With oRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = arrWords(i)
            
            .MatchWholeWord = True
            .Replacement.Text = arrWords(i) & arrInsertAfter(i)
   
            .Execute Replace:=wdReplaceAll
        End With
    Next

End Sub
any ideas are welcome thank you

dan
Reply With Quote
  #2  
Old 05-14-2016, 02:21 PM
macropod's Avatar
macropod macropod is online now Find only the first Instance of Words -Then Do Replacement Windows 7 64bit Find only the first Instance of Words -Then Do Replacement Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Change:
wdReplaceAll
to:
wdReplaceOne
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-14-2016, 04:34 PM
dan88 dan88 is offline Find only the first Instance of Words -Then Do Replacement Windows 10 Find only the first Instance of Words -Then Do Replacement Office 2016
Novice
Find only the first Instance of Words -Then Do Replacement
 
Join Date: Feb 2016
Posts: 24
dan88 is on a distinguished road
Default

Thanks Paul,

I missed that one right under my nose

My skills are not strong enough for the 2nd instance so ill leave it be

cheers

dan
Reply With Quote
  #4  
Old 05-14-2016, 08:10 PM
macropod's Avatar
macropod macropod is online now Find only the first Instance of Words -Then Do Replacement Windows 7 64bit Find only the first Instance of Words -Then Do Replacement Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by dan88 View Post
My skills are not strong enough for the 2nd instance so ill leave it be
If you want to replace the first two instances, simply use:
.Execute Replace:=wdReplaceOne
.Execute Replace:=wdReplaceOne
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 05-14-2016, 11:13 PM
gmayor's Avatar
gmayor gmayor is offline Find only the first Instance of Words -Then Do Replacement Windows 10 Find only the first Instance of Words -Then Do Replacement Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

While Paul's modification works fine, it's a very quiet Sunday morning so ... you could also use
Code:
Sub Insert_After_One_Instance()
Dim oRng As Word.Range
Dim arrWords As Variant
Dim arrInsertAfter As Variant
Dim i As Long
    arrWords = Array("Air Boat", "Power Boat", "Ferry")
    arrInsertAfter = Array("AB123", "PB456", "F123")

    For i = 0 To UBound(arrWords)
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(FindText:=arrWords(i), MatchWholeWord:=True)
                oRng.InsertAfter arrInsertAfter(i)
                Exit Do
            Loop
        End With
    Next i
    Set oRng = Nothing
End Sub
or for two instances (or more) add a counter e.g.
Code:
Sub Insert_After_Two_Instances()
Dim oRng As Word.Range
Dim arrWords As Variant
Dim arrInsertAfter As Variant
Dim i As Long, j As Long
    arrWords = Array("Air Boat", "Power Boat", "Ferry")
    arrInsertAfter = Array("AB123", "PB456", "F123")

    For i = 0 To UBound(arrWords)
        Set oRng = ActiveDocument.Range
        j = 0
        With oRng.Find
            Do While .Execute(FindText:=arrWords(i), MatchWholeWord:=True)
                oRng.InsertAfter arrInsertAfter(i)
                j = j + 1
                oRng.Collapse 0
                If j = 2 Then Exit Do 'Number of instances to process
            Loop
        End With
    Next i
    Set oRng = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #6  
Old 05-14-2016, 11:38 PM
macropod's Avatar
macropod macropod is online now Find only the first Instance of Words -Then Do Replacement Windows 7 64bit Find only the first Instance of Words -Then Do Replacement Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Or, to update just the 2nd instance:
Code:
Sub Demo()
Dim arrFnd, arrRep, i As Long, j As Long
arrFnd = Array("Air Boat", "Power Boat", "Ferry")
arrRep = Array("AB123", "PB456", "F123")
For i = 0 To UBound(arrFnd)
  j = 0
  With ActiveDocument.Range
    With .Find
      .Text = arrFnd(i)
      .MatchWholeWord = True
      .Wrap = wdFindStop
      .Execute
    End With
    Do While .Find.Found
      j = j + 1
      If j = 2 Then
        .InsertAfter arrRep(i)
        Exit Do
      End If
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
Next i
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 05-15-2016, 06:30 AM
dan88 dan88 is offline Find only the first Instance of Words -Then Do Replacement Windows 10 Find only the first Instance of Words -Then Do Replacement Office 2016
Novice
Find only the first Instance of Words -Then Do Replacement
 
Join Date: Feb 2016
Posts: 24
dan88 is on a distinguished road
Default

Hi Paul and Graham,

many thanks for extending the code in a more advanced direction.

I have found new combinations of replacements I can do now, which I didn't know was possible before.

The counter will be very handy

thanks ever so much again

very appreciative of your generous efforts

dan
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find only the first Instance of Words -Then Do Replacement VBA: Replacement of words based on a list roderh Excel Programming 2 09-06-2015 08:54 PM
Find only the first Instance of Words -Then Do Replacement Find-replace using part of what was found in the replacement text paulkaye Word 3 12-22-2014 02:52 AM
Find only the first Instance of Words -Then Do Replacement Automatic Words replacement psychologist Word VBA 3 11-22-2014 11:40 AM
Find instance of a word in a specific style and change its color hwg Word VBA 7 02-20-2014 10:59 PM
Highlight text and find next instance DrDOS Word 0 11-15-2010 04:02 PM

Other Forums: Access Forums

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


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