VBA比较Word文档中两个词的位置

VBA Compare positions of two words in a Word document

我正在编写一个用于在 Word 文档中定义首字母缩略词的模块。该脚本从 Excel 文档中获取首字母缩略词和定义。我遇到的麻烦是将首字母缩略词的第一个实例的位置与完整定义的第一个实例的位置进行比较。 最后,我需要确保首字母缩略词的第一个实例紧跟在定义的第一个实例之后,并括在括号中。完成后,脚本将需要删除定义的后续实例,因此我还需要弄清楚如何删除定义的第一个实例以外的所有实例。

最终结果应该是这样的:

....This document is about software as a service (SaaS). SaaS is software that is hosted by someone else. Rather than installing it on your own computer, you access it through a Web browser. There are many types of SaaS. ....

如何获取这两个元素的位置和/或比较它们的位置?

在上面的示例中,我如何找到 "SaaS" 的第一个实例并确保它恰好出现在 (space,左括号) 定义之后的两个位置(假设定义实际出现在文件中)?

'Selects first instance of acronym. Get start and end positions of first instance of acronym.
    Selection.HomeKey Unit:=wdStory
    Selection.Find.Execute Acronym 'Acronym is a variable. Now that it's selected, I need to get it's start position (or the position of the cursor if the cursor is at the start of the acronym) or find a way to compare it's position to the UserSelection variable.

    'Is the definition in the document?

        'If no, add definition before first instance of acronym.

        'If yes, get start and end positions of first instance of definition.

    'Is end position of first instance of definition adjacent to start position of first instance of acronym? If not, which is first?

        'If definition is first, add acronym behind definition.

        'If acronym is first, add definition in front of acronym and delete remaining instances of definition.

    'Highlights all instances of the acronym in green
        With ActiveDocument.Content.Find
            .MatchCase = True
            .MatchWholeWord = False
            With .Replacement
                .Highlight = True
            End With
            .Execute Replace:=wdReplaceAll, Wrap:=wdFindContinue, FindText:=Acronym, ReplaceWith:=Acronym
        End With

任何帮助或见解将不胜感激,因为我完全不知所措并且没有运气 Google。

-文斯

我认为以下代码片段可以帮助您:

Sub example(acronym, definition)

    Selection.Find.ClearFormatting
    With Selection.Find
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchAllWordForms = False
    End With

    ActiveDocument.Range(0, 0).Select      ' go to beginning of document
    Selection.Find.Text = acronym
    Selection.MatchSoundsLike = False
    If (Not Selection.Find.Execute) Then
        ' acronym not found in this document
        Exit Sub
    End If

    ActiveDocument.Range(0, 0).Select      ' go to beginning of document
    Selection.Find.Text = definition
    Selection.MatchCase = False
    Selection.MatchSoundsLike = True

    While (Selection.Find.Execute)
        '
        Selection.Delete                    ' delete all definitions
        '
    Wend

    ActiveDocument.Range(0, 0).Select       ' go to beginning of document
    Selection.Find.Text = acronym
    Selection.MatchSoundsLike = False
    If (Selection.Find.Execute) Then
        Selection.InsertBefore "(" & definition & ")"
    End If

End Sub

注意:我还发现作者在定义中犯了错误(微小的变化),甚至一个额外的无意 space 搞乱了发现。