word 2010 VBA macro question - Search for word, extend selection, then delete

I would like to have a macro that:
1.  Searches for the character string FIG.
2.  I would like to select the character string FIG and continue selecting characters to the right until the hard return character is reached.
3.  I would like to move backwards one character because I do  NOT want to select the hard return character. I'm just using the hard return character as a marker for how long I would like my selection to be.
4. I would like to pause and have a pop up message appearing that says "erase?"
5. I would like to delete the selected character string if I click the "yes" button. And I'd like to terminate the macro if I click the "no" button.
I do not need to loop or do until, etc.
brothertruffle880Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
This should do that
Sub DelFig()
    Dim rng As Range
    Set rng = ActiveDocument.Range
    With rng.Find
        .MatchWildcards = True
        .Text = "FIG*^13"
        If .Execute() Then
            If MsgBox("Delete?", vbYesNo) Then
                rng.MoveEnd wdCharacter, -1
                'rng.Select 'If you need to see what is being deleted, remove the first commenting apostrophe from this line
                rng.Delete
            End If
        End If
    End With
End Sub

Open in new window

3

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brothertruffle880Author Commented:
GrahamSkan:
Wonderful solution. Thanks!
0
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
cool code, Graham ~ if one did want to loop, what would need to change? thanks
0
GrahamSkanRetiredCommented:
It gets a bit more complicated, but the Find.Text remains the same. I've added a report message at the end.
Sub DelFigs()
    Dim rng As Range
    Dim f As Integer
    Dim d As Integer

    Set rng = ActiveDocument.Range
    Do While True
        With rng.Find
            .MatchWildcards = True
            .Text = "FIG*^13"
            If .Execute() Then
                f = f + 1
                rng.MoveEnd wdCharacter, -1
                rng.Select 'show text to be deleted
                Select Case MsgBox("Delete?", vbYesNoCancel)
                    Case vbYes
                        rng.Delete
                        d = d + 1
                    Case vbNo
                    Case vbCancel
                        Exit Do
                End Select
                'start from current position to avoid refinding non-deleted text
                rng.Start = rng.End
                rng.End = ActiveDocument.Range.End
            Else
                Exit Do
            End If
        End With
    Loop
    MsgBox "Finished. Found Count: " & f & ". Deleted count: " & d
End Sub

Open in new window

1
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
thank you, Graham. My purpose is to color comments in pasted code green. Here is the final procedure:
Sub ColorCommentsAllGreen()
'170415 based on code by Graham Skan
'modified by crystal

   On Error GoTo Proc_Err

   Dim nCount As Long
   
   Dim rng As Range
   
   nCount = 0
   Set rng = ActiveDocument.Range
    
   Do While True
      With rng.Find
         .MatchWildcards = True
         .Text = "'*^13"
        If .Execute() Then
            rng.Font.Color = 26112 'dark Green
            nCount = nCount + 1
            'start from current position to avoid refinding text
            rng.Start = rng.End
            rng.End = ActiveDocument.Range.End
        Else
           Exit Do
        End If
      End With
   Loop
   MsgBox "Colored " & Format(nCount, "#,##0") & " comments", , "Done"
   
Proc_Exit:
   On Error Resume Next
   Set rng = Nothing
   Exit Sub
  
Proc_Err:
   MsgBox Err.Description, , _
        "ERROR " & Err.Number _
        & "   ColorCommentsAllGreen"

   Resume Proc_Exit
   Resume
End Sub

Open in new window

obviously this isn't perfect since I am not checking for a single quote in the code, just assuming it is the start of a comment, which it usually is ...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.