以下是一个用 VBA 实现在 Word 中逐句查找带有文本突出颜色的词汇并用';'隔开每个查找到的整体的例子:

Sub FindHighlightedWords()
    Dim doc As Document
    Dim rng As Range
    Dim sentence As Range
    Dim highlightedWords As String
    
    ' 获取当前活动文档
    Set doc = ActiveDocument
    
    ' 初始化查找范围为整个文档
    Set rng = doc.Range
    
    ' 循环遍历每一句话
    Do While rng.Find.Execute
        ' 获取当前句子的范围
        Set sentence = rng.Sentences(1)
        
        ' 检查当前句子是否有文本突出颜色
        If sentence.Font.Color <> wdColorAutomatic Then
            ' 遍历句子中每个单词
            For Each word In sentence.Words
                ' 检查单词是否有文本突出颜色
                If word.Font.Color <> wdColorAutomatic Then
                    ' 把查找到的单词添加到结果字符串中,用';'隔开
                    highlightedWords = highlightedWords & word.Text & ';'
                End If
            Next word
        End If
        
        ' 移动到下一句话
        Set rng = sentence.End + 1
    Loop
    
    ' 去除最后一个';'
    highlightedWords = Left(highlightedWords, Len(highlightedWords) - 1)
    
    ' 在消息框中显示结果
    MsgBox highlightedWords
End Sub

请注意,此代码假设文本突出颜色不是自动颜色(wdColorAutomatic)。如果文本突出颜色是自动的,则需要相应地修改代码。


原文地址: https://www.cveoy.top/t/topic/o5p2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录