PDA

View Full Version : Find and Replace with Wildcards and yes no cancel



JPG
10-11-2020, 06:26 AM
I found and example for a yes no confirmation, needs a cancel button as well.
But my question is how do I get this working with the wildcard search I have. The back references don't appear to work even though this is a wildcard=true.
Any help will be of great value.
Yes I have tried searching this forum and no I have not posted this on any other forum and no I am not lazy, I just need some help.


Private Sub PromptForReplace()


Dim myRange As Range


Set myRange = ActiveDocument.Content
myRange.Find.ClearFormatting
myRange.Find.MatchWildcards = True


Dim cached As Long
cached = myRange.End
Do While myRange.Find.Execute("([a-z])^13([A-Za-z])")
myRange.Select
If MsgBox("Replace " & myRange.Find.text & "?", vbYesNo) = vbYes Then
myRange.text = "\1 \2"
End If
myRange.start = myRange.start + Len(myRange.Find.text)
myRange.End = cached
Loop


End Sub

gmaxey
10-11-2020, 07:33 AM
Private Sub PromptForReplace()
Dim oRng As Range
Set oRng = ActiveDocument.Content
Selection.Find.ClearFormatting
With oRng.Find
.Text = "([a-z])^13([A-Za-z])"
.MatchWildcards = True
Do While .Execute
oRng.Select
Select Case MsgBox("Replace " & oRng & "?", vbYesNoCancel)
Case vbYes: oRng.Text = Replace(oRng.Text, Chr(13), " ")
oRng.Collapse wdCollapseEnd
Case vbNo:
oRng.Collapse wdCollapseEnd
Case Else
Selection.Collapse wdCollapseStart
Exit Do
End Select
Loop
End With
lbl_Exit:
Exit Sub
End Sub

JPG
10-11-2020, 09:01 AM
Greg, thanks for taking the time to help me. It works just as I need.
I will put attribution to you in the macro and a link here for future reference.

gmaxey
10-11-2020, 09:14 AM
JPG,

I don't need attribution. I stopped caring about gold stars by my name in grammar school. Thanks though.