I hope this message finds you well. I was wondering if I could kindly ask for your assistance. I am currently working on developing a word macro that involves finding and replacing specific text with the help of wildcards based on an excel file. I have managed to create a macro that successfully accomplishes this task:
Code: Select all
Sub FNR_Red()
ActiveDocument.TrackRevisions = False
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 doc = ActiveDocument
Set xlApp = CreateObject("Excel.Application")
Set xlWbk = xlApp.Workbooks.Open("D:\replace.xlsx")
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
.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
ActiveDocument.TrackRevisions = False
End Sub
Original paragraph:
Video provides a powerful way to help you prove your point. When 中田英壽 click Online Video, you can paste in the embed code for the video you want to add. You can also type a keyword to search online for the video that best fits your document.
To make your document look professionally produced, Word provides header, footer, cover page, and text box designs that complement each other. For example, you can add a matching cover page, header, and sidebar. Click Insert and then choose the elements you want from the different galleries.
Themes and styles also help keep your document coordinated. When 中田英壽 click Design and choose a new Theme, the pictures, charts, and SmartArt graphics change to match your new theme. When 中田英壽先生 apply styles, your headings change to match the new theme.
After running the macro:
Video provides a powerful way to help you prove your point. When 中田英壽先生 click Online Video, you can paste in the embed code for the video you want to add. You can also type a keyword to search online for the video that best fits your document.
To make your document look professionally produced, Word provides header, footer, cover page, and text box designs that complement each other. For example, you can add a matching cover page, header, and sidebar. Click Insert and then choose the elements you want from the different galleries.
Themes and styles also help keep your document coordinated. When 中田英壽先生 click Design and choose a new Theme, the pictures, charts, and SmartArt graphics change to match your new theme. When 中田英壽先生 apply styles, your headings change to match the new theme.
The text after "中田英壽" will also changed in wdRed. This is the issue due to my wildcard setting in the excel file:
Column A: [<中][田][英][壽>]([!先])
Column B: 中田英壽先生\1
Column C: T
(Column C put a "T" to inform the macro wildcard function should be used.)
What I expect is to only find "中田英壽" and replace as "中田英壽先生". If "中田英壽先生" existed, do not replace it.
May I ask if anyone can kindly assist me in achieving this task? Thank you very much in advance.
(Attached please find the excel template)