Thread: [Solved] Word macro wildcard issue
View Single Post
 
Old 01-11-2024, 03:21 AM
syl3786 syl3786 is offline Windows 10 Office 2019
Advanced Beginner
 
Join Date: Jan 2023
Posts: 97
syl3786 is on a distinguished road
Default Word macro wildcard issue

Greetings everyone,

I hope this message finds you well. I was wondering if I could kindly ask for your assistance. I am currently developing a word macro that will find and replace Arabic numbers to Chinese numbers with tracked changes record. If it's not too much trouble, please see the code below:

Code:
Sub ANC()

Dim RT As Boolean
   
RT = ActiveDocument.TrackRevisions
ActiveDocument.TrackRevisions = True

Selection.HomeKey Unit:=wdStory, Extend:=wdMove
        
With Selection.Find

        .ClearFormatting
        .Replacement.ClearFormatting
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchWildcards = True
    
        .text = vbTab & "1{1}([!0-9])"
        .Replacement.text = vbTab & "一\1"
        .Replacement.Font.ColorIndex = wdRed
        .Execute Replace:=wdReplaceAll
    
End With
    
ActiveDocument.TrackRevisions = RT

End Sub
For example:

Original paragraph:

[vbTab] 1abc, Video provides a powerful way to help you prove your point. When you 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.

After running the macro:

[vbTab] 一abc, Video provides a powerful way to help you prove your point. When you 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.

I have noticed that the text after "1" is also being changed in wdRed. From what I understand, this is happening because the wildcard "([!0-9])" is occupying a place and when it replaces according to the "\1", it is producing the undesired outcome as mentioned above.

Another issue that I am encountering is that any year, such as "1987" after vbTab, is also being changed to "一987".

If it's not too much trouble, could you please guide me on how to solve the above issues? Your expertise would be greatly appreciated.

Thank you very much for your time and consideration.
Reply With Quote