将引号内的字符串匹配到 Word 中引号外的字符串

Match string within quotation marks to string out of quotation marks in Word

我有一份文档,其中包含已定义术语的索引。每个定义的术语都可以在引号内找到。无论在文档中实际使用该术语的什么地方,它都不用引号引起来。在文档中使用每个术语的地方,我想更改其格式(将文本设为浅灰色)。通过这种方式,我可以很容易地看到我的文档中还有哪些单词尚未定义(如果它们已经定义,它们将是苍白的,我不会注意到它们......深色文本突出)例如:

"Cat" 表示猫科动物。 "Hat"表示头部装备。

猫戴着帽子。

一旦宏是 运行:

就会转向这个(我使用斜体而不是灰色字体,因为我不知道如何在这里更改字体颜色)

"Cat" 表示猫科动物。 "Hat"表示头饰。

戴着一顶帽子

我知道如何在 Word 中使用通配符来搜索引号内的所有单词,但是我却不知道如何立即找到所有这些单词并将其替换为不同的字体。我一直在为每个定义的术语使用“查找和替换”,并且需要数小时才能将所有术语都变灰...

嗯,现在我比以前更了解 Word 中的 Find()...

这对我来说适用于轻度测试,但只能处理单词术语的简单用例,其中没有术语是另一个术语的子字符串。

Sub Tester()

    Dim col As New Collection
    Dim wd, l
    Dim rng As Range, doc As Document

    Set doc = ThisDocument
    Set rng = doc.Content

    'collect all quoted terms
    With rng.Find
        .MatchWildcards = True
        .MatchCase = False
        .Forward = True
        'matching straight or curly quotes...
        Do While .Execute(FindText:="[""" & Chr(147) & "][a-zA-Z]{1,}[""" & Chr(148) & "]")
            wd = Mid(rng.Text, 2, Len(rng.Text) - 2)
            'skip error if already added
            On Error Resume Next
            col.Add wd, wd
            If Err.Number = 0 Then Debug.Print "Quoted:", wd
            On Error GoTo 0
        Loop
    End With

    'search for each quoted term
    For Each wd In col
        Debug.Print "Searching:", wd

        Set rng = doc.Content
        With rng.Find

            .MatchCase = False
            .MatchWildcards = True
            .MatchWholeWord = True
            .Forward = True

            'the only issue here is that the Find is case-sensitive...
            'which is why we need to check for both the init-cap and lower-case versions
            l = Left(wd, 1)
            wd = "[" & LCase(l) & UCase(l) & "]" & Right(wd, Len(wd) - 1)
            Do While .Execute(FindText:="[!""" & Chr(147) & "]" & wd & "[!""" & Chr(147) & "]")

                Debug.Print "  Found:", wd
                rng.Font.ColorIndex = wdGray25

            Loop
        End With
    Next

End Sub