【问题标题】:Match string within quotation marks to string out of quotation marks in Word将引号内的字符串与Word中的引号外的字符串匹配
【发布时间】:2015-07-30 11:10:03
【问题描述】:

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

“猫”是指猫科动物。 “帽子”是指头饰。

猫戴着帽子。

一旦宏运行,就会转向这个(我使用斜体而不是灰色字体,因为我无法弄清楚如何在此处更改字体颜色):

”是指猫科动物。 “帽子”表示头饰。

戴着一顶帽子

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

【问题讨论】:

  • 我认为您无法按照自己的方式进行操作。向我们展示需要数小时才能运行的代码,并告诉我们文本有多大。我认为,您必须逐项进行,但这并不意味着它必须很慢。

标签: vba replace find wildcard defined


【解决方案1】:

嗯,现在我对 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

【讨论】:

  • 蒂姆,你真的为我提供了一个很好的起点。非常感谢,非常好意。伊冯娜
猜你喜欢
  • 2014-05-02
  • 1970-01-01
  • 1970-01-01
  • 2011-01-30
  • 1970-01-01
  • 2010-10-13
  • 1970-01-01
  • 2018-12-21
  • 1970-01-01
相关资源
最近更新 更多