Avatar of brothertruffle880
brothertruffle880
Flag for United States of America asked on

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.
VBAMicrosoft Word

Avatar of undefined
Last Comment
crystal (strive4peace) - Microsoft MVP, Access

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
GrahamSkan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
brothertruffle880

ASKER
GrahamSkan:
Wonderful solution. Thanks!
crystal (strive4peace) - Microsoft MVP, Access

cool code, Graham ~ if one did want to loop, what would need to change? thanks
GrahamSkan

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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
crystal (strive4peace) - Microsoft MVP, Access

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