Consulting

Results 1 to 4 of 4

Thread: Find and Replace with Wildcards and yes no cancel

  1. #1
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location

    Find and Replace with Wildcards and yes no cancel

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    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.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    JPG,

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

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •