Hi I need a macro to delete everything from the beginning of my document up to certain words
Hi I need a macro to delete everything from the beginning of my document up to certain words
Simple enough
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
Graham Mayor - MS MVP (Word) 2002-2019
Visit my web site for more programming tips and ready made processes
http://www.gmayor.com
Thank you VBAX Master for your code. It works very well for me.
Here is another way:
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