Find and replace in word not working

Rafaelete

New Member
Joined
Feb 2, 2022
Messages
14
Office Version
  1. 2007
Platform
  1. Windows
Hello, I have a problem with a find and replace in word using excel vba.

The problem I have is with trackchanges. I have to active it due to the requirements of the automatization, but I´m finding myself with two words been changed twice. When I do it without trackchanges it works just fine.

This is an example of the problem I have. These are words that I´m replacing:

BuscarReemplazar
Trading - Energy Manag.IbeEM Iberia - Strategic Ibe
Trading - Iberia (Operations)REVISAR
Trading - Iberia (Int. & Coord. Inf.)REVISAR
REVISARREVISAR
Responsable actividadREVISAR
Global Hub - AdoptionREVISAR
Global AdoptionDISTINTOS NIVELES
EM - IbeREVISAR
EM InfREVISAR
EM - OperationsREVISAR
EM Ibe - OperationsREVISAR
AFC - Administration Iberia - Accounting Iberia (Energetic Business)ADM Iberia - Accounting Ibe - Negocios Energéticos
AFC - Administration Iberia - Accounting IberiaBUSTED
ADM EnergíaREVISAR
AA2REVISAR
AA1REVISAR
Activity OwnerREVISAR


The problem I´m having is with these two words:
It changes the first one just fine, but then it changes the second one in the same words as if it wasn´t changed.
AFC - Administration Iberia - Accounting Iberia (Energetic Business)ADM Iberia - Accounting Ibe - Negocios Energéticos
AFC - Administration Iberia - Accounting IberiaBUSTED

This is the result I´m getting:
1652372976079.png


Without trackchanges, it does the change without any problems:
1652372862789.png



I need to get this result with trackchanges as true. But I´m unable to do it.

This is the code I´m using:

Dim Wbk As Workbook: Set Wbk = ThisWorkbook
Dim Wrd As New Word.Application
Dim Dict As Object
Dim RefList As Range, RefElem As Range
Dim Key
Dim wrdRng As Range
Dim WDoc As Document


Wrd.Visible = True

Set WDoc = Wrd.Documents.Open(filename:=sFileName, OpenAndRepair:=True) 'Modify as necessary.

Debug.Print sFileName

' Activar control de cambios en cada documento
With WDoc:
.TrackRevisions = False
WDoc.ActiveWindow.View.MarkupMode = wdBalloonRevisions
End With

'Assigns the columns that is going to have the original texts that need to be changed
Set Dict = CreateObject("Scripting.Dictionary")
Set RefList = Wbk.Sheets("Reemplazos").Range("B2:B50") 'Modify as necessary.


'Selects the column that´s one column to the right of the reference column
With Dict
For Each RefElem In RefList
If Not .Exists(RefElem) And Not IsEmpty(RefElem) Then
.Add RefElem.Value, RefElem.Offset(0, 1).Value
Debug.Print RefElem
End If
Next RefElem
End With

'Assigns the conditions and loops through each text to replace it
For Each Key In Dict
With WDoc.Content.FIND
Debug.Print Key
.Execute MatchAllWordForms:=False
.Execute Forward:=True
.Execute Wrap:=wdFindAsk
.Execute Format:=False
.Execute MatchCase:=False
.Execute MatchWildcards:=False
.Execute MatchSoundsLike:=False
.Execute wdReplaceAll
.Execute FindText:=Key, ReplaceWith:=Dict(Key), Replace:=2
End With
Next Key



Any help will be greatly appreciated. Thanks a lot in advance.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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