Hi I need a macro to delete everything from the beginning of my document up to certain words
Printable View
Hi I need a macro to delete everything from the beginning of my document up to certain words
Simple enough
Code:Sub DeleteToPhrase()
Const strPhrase As String = "the phrase to find"
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:=strPhrase, MatchCase:=True)
oRng.Collapse 1
oRng.start = ActiveDocument.Range.start
oRng.Text = ""
Exit Do
Loop
End With
lbl_Exit:
Exit Sub
End Sub
Thank you VBAX Master for your code. It works very well for me.
Here is another way:
Code:Sub DeleteToPhrase()
Const strPhrase As String = "the phrase to find"
Dim oRng As Range
Set oRng = ActiveDocument.Range
oRng.End = InStr(oRng.Text, strPhrase) - 1
If Len(oRng) > 1 Then oRng.Delete
lbl_Exit:
Exit Sub
End Sub