![]() |
|
#1
|
|||
|
|||
![]() Hi! This is the fastest way, otherwise you'll have to work with a range & then move it one char to the left in each loop: Code:
With selection.Find .ClearFormatting .Replacement.ClearFormatting .Forward = True .Wrap = wdFindStop .Format = True .MatchWildcards = True .text = Chr(9) & "1([!0-9])" .Replacement.text = Chr(9) & "-\1" .Execute Replace:=wdReplaceAll .text = Chr(9) & "-" .Replacement.text = "" .Replacement.Font.ColorIndex = wdRed .Execute Replace:=wdReplaceAll End With |
#2
|
|||
|
|||
![]() Quote:
|
#3
|
|||
|
|||
![]() Quote:
For example: Code:
Sub test() ' Declare variables Dim doc As Document Dim xlApp As Object Dim xlWbk As Object Dim xlSht As Object Dim rngFind As range Dim rngReplace As range Dim strFind As String Dim strReplace As String ' Set the document object Set doc = ActiveDocument ' Open the Excel workbook Set xlApp = CreateObject("Excel.Application") Set xlWbk = xlApp.Workbooks.Open("C:\Names.xlsx") ' Set the worksheet object Set xlSht = xlWbk.Sheets(1) ' Loop through each row in the worksheet For i = 1 To xlSht.UsedRange.Rows.Count ' Get the find and replace strings from the worksheet strFind = xlSht.Cells(i, 1).Value strReplace = xlSht.Cells(i, 2).Value ' Find and replace the text in the document With doc.Content.Find .ClearFormatting .Text = strFind .Replacement.ClearFormatting .Replacement.Text = strReplace ' Set the font color to red .Replacement.Font.Color = wdColorRed ' Find and replace while preserving the font color .Format = True .Wrap = wdFindStop .MatchCase = False .MatchWholeWord = False .MatchWildcards = True .Execute Replace:=wdReplaceAll, Format:=True End With Next i ' Close the Excel workbook xlWbk.Close SaveChanges:=False ' Quit the Excel application xlApp.Quit The macro needs to find "ABC (without Member)" (which is ABC[!Member] in wildcard), and replace as ABCMember. |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Recording macro for Find Wildcard | 2vbr | Word VBA | 3 | 09-21-2020 05:29 PM |
![]() |
MaryTom | Word VBA | 2 | 05-18-2018 04:23 PM |
Issue with wildcard search | mysterytramp | Word | 0 | 05-13-2015 10:40 AM |
Issue with Wildcard Search and Replace | linusthedog | Word | 3 | 09-04-2014 03:32 PM |
Change characters outside a wildcard while keeping wildcard results | nymusicman | Word VBA | 2 | 04-10-2014 08:17 AM |