Find and Replace Array in MS Word, Runs but doesn't Work

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,461
I have been trying to get this simple find and replace macro to work but have been unsuccessful. I have no experience with VBA in word so I am unsure where I am going wrong. Any help would be much appreciated. Here is what I have. I tried to comment it to make it easier to understand what I am attempting to do:

Code:
Sub ReplaceWords()

 Dim vFindText    As Variant
 Dim vReplText    As Variant
 Dim i                 As Long
 

 vFindText = Array("uc14-*", "rf14-*", "sa14-*", "SKAT")  [COLOR=#008000]'Set Find Array[/COLOR]
 
 vReplText = Array("", "", "", "")  [COLOR=#008000]'Set Replacement Array[/COLOR]
  
  Selection.WholeStory [COLOR=#008000]'Select All Text in Document[/COLOR]
  With Selection.Find
 .Forward = True
 .Wrap = wdFindContinue
 .MatchWholeWord = True
 .MatchWildcards = False
 .MatchSoundsLike = False
 .MatchAllWordForms = False
 .Format = True
 .MatchCase = True
 
[COLOR=#008000] 'Replace each item from the first array with the corresponding item in the second array[/COLOR]
 For i = LBound(vFindText) To UBound(vFindText) 
 .Text = vFindText(i)
 .Replacement.Text = vReplText(i)
 .Execute Replace:=wdReplaceAll
 
 Next i
 End With
 
 End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Your code works for me. Just few code tweaks:
Code:
Sub ReplaceWords()
Application.ScreenUpdating = False
Dim vFindText    As Variant
Dim vReplText    As Variant
Dim i                 As Long
 
vFindText = Array("uc14-*", "rf14-*", "sa14-*", "SKAT")  'Set Find Array
vReplText = Array("", "", "", "")  'Set Replacement Array

With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Format = False
  .Forward = True
  .MatchCase = True
  .Wrap = wdFindContinue
  .MatchWholeWord = True
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  
  'Replace each item from the first array with the corresponding item in the second array
  For i = LBound(vFindText) To UBound(vFindText)
    .Text = vFindText(i)
    .Replacement.Text = vReplText(i)
    .Execute Replace:=wdReplaceAll
  Next i
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,684
Messages
6,186,426
Members
453,354
Latest member
Ubermensch22

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top