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.Delete
ought to work, as long asFind
was successful - otherwise something else would be deleted.myRange.Delete
would also work, as long as you test whetherFind.Execute
returns True. So on the whole, Vityata's suggestion is the best way. - Cindy Meistertxt1 & ^p
- Cindy Meister