Using VBA regular expressions to find/replace in Word

妖精的绣舞 提交于 2021-01-29 09:51:58

问题


Yes, I have searched (and found) many response similar to this, but none work for me. I have a lot of patterns to replace, most along these lines:

Find: ([aA])ttorney-at-law Replace: \1ttorney at law

I could create two ordinary find/replaces for each case, but I've got hundreds to do. Any help or direction would be much appreciated. I can do this in NPP, but the people I'm doing this for work in Word exclusively.

I've got other phrases where I'm adding hyphens or, e.g., changing ([bB])ook store (two words) to \1ookstore (one word, retaining original case).

I got this suggestion elsewhere, and just plugged my patterns in, but it doesn't do anything:

Sub SearchReplace()

    Dim regEx As Object
    Dim qtext As String
    Dim html_input As String

    ' set up regex
    Set regEx = CreateObject("VBScript.RegExp")
    regEx.Pattern = "\(\[aA\]\)ttorney-at-law"
    regEx.Global = True

    ' input and replacement text
    qtext = "\(\[aA\]\)ttorney-at-law"
    html_input = "\1ttorney at law"

    ' do replace
    qtext = regEx.Replace(qtext, html_input)

End Sub

Thank you!

来源:https://stackoverflow.com/questions/50274476/using-vba-regular-expressions-to-find-replace-in-word

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!