Trying to search a word document through excel VBA and delete the found text:
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
path = "C:\Test.docx"
txt1 = "Search text"
Set objDoc = objWord.Documents.Open(path)
With objWord.ActiveDocument
Set myRange = .Content
With myRange.Find
.Text = txt1
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Execute
Selection.Delete
End With
End With
However, it doesn't seem to delete the text!
Any help please
objWord.Selection.Find(which I don't recomment) thenSelection.Deleteought to work, as long asFindwas successful - otherwise something else would be deleted.myRange.Deletewould also work, as long as you test whetherFind.Executereturns True. So on the whole, Vityata's suggestion is the best way. - Cindy Meistertxt1 & ^p- Cindy Meister