View Single Post
 
Old 06-22-2022, 03:53 AM
marcotte974 marcotte974 is offline Windows 8 Office 2013
Novice
 
Join Date: Jun 2022
Posts: 2
marcotte974 is on a distinguished road
Default Find and replace text in a document with wildcards in vba

Salut ŕ toute la communauté
To find generic words in a word document and surround them with square brackets I use the following macro and it works:
Code:
Sub test()
Dim rngDoc As Word.Range
Dim sArray As Variant, tArray As Variant
Dim i As Long
sArray = Array("ABCD.0001/0002/0003")
tArray = Array("ABCD.0001", "ABCD.0002", "ABCD.0003")

Set rngDoc = ActiveDocument.Range
For i = 0 To UBound(sArray)
    With rngDoc.Find
        .ClearFormatting
        .Text = sArray(i)
        .Replacement.Text = "ABCD.0001 ABCD.0002 ABCD.0003"
        .Execute Replace:=wdReplaceAll
    End With
Next
For i = 0 To UBound(tArray)
    With rngDoc.Find
        .ClearFormatting
        .Text = tArray(i)
        .Replacement.Text = "[" & tArray(i) & "]"
        .Execute Replace:=wdReplaceAll
    End With
Next
 End Sub
Is it possible to replace my tArray with an expression like "ABCD.[0-9]{4}" because my generic words can go from ABCD.0001 to ABCD.9999?
Thank you
Reply With Quote