We help IT Professionals succeed at work.

MS Word 2013 VBA: I would like to search for a word, go to the beginning of the line. select the data until the hard return (paragraph symbol)

brothertruffle880
on
Please help me write a macro.

I would like to:
1. search for the word degree.
2. stop the search.
3. bring the cursor to the beginning of the line.
4. select the line up to and including the hard return (paragraph symbol)
5. Delete that selection
6. prompt message box:  Would you like to repeat this macro starting at step 1. YES (go to step 1) NO (end macro)

for some reason my macro recorder does not record "searching".
Comment
Watch Question

Production Manager
Commented:
Sorry, part of your request is not possible. The VBA object model doesn't know where a line of text starts, because line is not a Word object. Word understands characters, words, sentences, paragraphs and sections. So the closest is to move the range to the start of the sentence. Here's the code:
Sub DeleteDegrees()
    Dim oRange As Range
    Dim SearchTerm$

    SearchTerm$ = "degree"

    Set oRange = ActiveDocument.Content
    oRange.Find.ClearFormatting
    oRange.Find.Replacement.ClearFormatting
    With oRange.Find
        .Text = SearchTerm$
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False '
    End With

    oRange.Find.Execute
    While oRange.Find.Found
        With oRange
            .MoveStart Unit:=wdSentence, Count:=-1
            .MoveEnd wdParagraph, 1
            .Delete
        End With
        Choice = MsgBox("Continue?", vbQuestion + vbYesNo + vbDefaultButton2, "Remove degree mentions")
        If Choice = vbYes Then
            oRange.Find.Execute
        Else
            Exit Sub
        End If
    Wend
End Sub

Open in new window

Rob HensonFinance Analyst
Commented:
Replicating pressing Home key will take you to the start of the line, Shift + End will then select the whole line