1

我需要为 Microsoft Word 文档中的每个单词添加正确的重音符号。我有一个名为“DoAccentuate”的脚本,它确定输入的任何单词的正确重音。但是,我不知道如何捕获当前选择的找到的单词,用我的 DoAccentuate 脚本处理它,然后用结果替换同一个单词(不影响文本的格式)。这是我尝试过的。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim document As Word.Document
    document = Globals.ThisAddIn.Application.ActiveDocument

    Dim FindObject As Word.Find = document.Application.Selection.Find
    With FindObject
        .ClearFormatting()
        .Text = "<*>"
        .MatchWildcards = True
        .Replacement.ClearFormatting()
        .Replacement.Text = DoAccentuate(document.Application.Selection.Text)
        .Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)

    End With
End Sub
4

1 回答 1

1

现在让我们假设这是您的 DoAccentuate 脚本:

Private Function DoAccentuate(theWordToAccentuate As String)
    theWordToAccentuate = theWordToAccentuate + "`"
    DoAccentuate = theWordToAccentuate
End Function

鉴于此,查找/替换可以很简单:

Public Sub FindWordAndReplaceWithAccentuatedForm()
    Dim accentuatedText As String: accentuatedText = DoAccentuate(Selection.text)

    Selection.Find.text = Selection.text
    Selection.Find.Replacement.text = DoAccentuate(Selection.text)
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

这不应删除任何格式。

我假设您需要一个解决方案,该解决方案要求用户选择他们想要强调的单词,然后运行宏(因为您上面的方法假设了这一点,即:.Replacement.Text = DoAccentuate(document.Application.Selection.Text

于 2013-04-07T05:25:06.717 回答