vb.net - 更好的方法来重读Word文档中的每个单词?

标签 vb.net ms-word office-interop

我是编程新手,但我正在尝试将现有脚本改编为 MS Word 2010/2013 插件,以便为打开文档中的每个拉丁单词添加正确的重音强调。

脚本“DoAccentuate”为我作为字符串发送的任何非重音拉丁单词返回一个重音单词。我只需要帮助更好地循环所有单词,然后在到达最后一个单词时停止循环。我当前的方法有点愚蠢...我在文档末尾插入一个无意义的单词,然后循环直到它被选中并重音。

也许有更好或更有效的方法来完成整个事情。

    Public Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
    Dim document As Word.Document
    document = Globals.ThisAddIn.Application.ActiveDocument
    Dim mySelection = document.Application.Selection
 'make sure cursor is at start of document
      document.Application.Selection.HomeKey(Unit:=Microsoft.Office.Interop.Word.WdUnits.wdStory)

    'insert fake word at end to stop the loop
    Dim range As Word.Range
    range = document.Range()
    range.InsertAfter(" documentendatoris")

    Do
        'use MS Word's wildcard to select the first individual word as trimmed string
        mySelection.Find.Text = "<*>"
        mySelection.Find.MatchWildcards = True
        mySelection.Find.Execute()
        'replace the selected word that has been found with its accented counterpart
        mySelection.Text = Accentuate.Accentuate.DoAccentuate(mySelection.Text)
    Loop Until mySelection.Text = "documentendatóris"

End Sub

最佳答案

嗯,我真的不知道它是否更有效,但你可以使用document.Contentrange.Words 收集检查主要故事范围内的所有单词

    document = Globals.ThisAddIn.Application.ActiveDocument

    Dim range As Word.Range
    range = document.Content

    Dim current As Integer
    current = 0

    Dim words As Word.Words
    words = range.Words

    Dim word As Word.Range

    Do
        current = current + 1
        If current < words.Count Then
            word = words(current)
            If word.Text.EndsWith(" ") Then
                word.Text = word.Text.Trim() + "'s "
                'replace the selected word that has been found with its accented counterpart
                'mySelection.Text = Accentuate.Accentuate.DoAccentuate(mySelection.Text)
            Else
                word.Text = word.Text.Trim() + "'s"
            End If
        End If
    Loop Until current = words.Count

关于vb.net - 更好的方法来重读Word文档中的每个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15861626/

相关文章:

vb.net - 如何在 VB.Net 中向 DataGridView 添加记录?

asp.net - 在 .NET 中检查两个 double 的相等性时出现问题——这个方法有什么问题?

c# - 在 MS Word 中填写表格需要花费大量时间

c# - 如何在不显示 Excel 窗口的情况下按文件名打开工作簿?

c# - 如何使用C#在word 2007文档中的特定坐标处插入图片/文本标签?

c# - 这是 Me.Top 的 .NET Framework 错误吗?

vb.net - 计算多维数组中的项目

ms-access - 将 MS Word 表单域导入 MS Access

svn - 使用 TortoiseSVN 合并 Microsoft Word 文档

c# 在 OneNote 中打开 "Select location"窗口