I’ve got a simple VBA code which is supposed to open a Word file and replace text “<1>” with text “<2>”,then save file with a new name but it doesn’t do that. Instead it would save the file without changes. In the same time result of “Execute” would be positive(Message box with text “Found” is displayed).
On Error Resume Next
Dim WordApp As Object
Set WordApp = GetObject(, "Word.Application")
If WordApp Is Nothing Then
Set WordApp = CreateObject("Word.Application")
End If
On Error GoTo 0
WordApp.documents.Open Filename:="C:UsersAdminDesktopTestTest.docx", ReadOnly:=False
With WordApp.ActiveDocument.Content.Find
.Text = "<1>"
.Replacement.Text = "<2>"
.Replacement.ClearFormatting
.Replacement.Font.Italic = False
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
If .Found Then
MsgBox "Found!"
Else
MsgBox "Not found"
End If
End With
WordApp.ActiveDocument.SaveAs Filename:="C:UsersAdminDesktopTestTest2.docx"