Link to home
Start Free TrialLog in
Avatar of brothertruffle880
brothertruffle880Flag 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.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brothertruffle880

ASKER

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

cool code, Graham ~ if one did want to loop, what would need to change? thanks
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

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