Consulting

Results 1 to 4 of 4

Thread: Remove all before certain sentence

  1. #1
    VBAX Regular
    Joined
    Sep 2007
    Location
    Virginia
    Posts
    49
    Location

    Remove all before certain sentence

    Hi I need a macro to delete everything from the beginning of my document up to certain words

  2. #2
    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

  3. #3
    VBAX Regular
    Joined
    Sep 2007
    Location
    Virginia
    Posts
    49
    Location
    Thank you VBAX Master for your code. It works very well for me.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •