Vb.net 强调word文档中每个单词的更好方法?

Vb.net 强调word文档中每个单词的更好方法?,vb.net,ms-word,office-interop,Vb.net,Ms Word,Office Interop,我是编程新手,但我正在尝试将现有脚本改编为MS Word 2010/2013 addin,以便在开放文档中的每个拉丁单词中添加正确的重音 脚本“doAccectuate”为我作为字符串发送的任何不带重音的拉丁单词返回一个带重音的单词。我只是需要帮助做一个更好的工作,循环所有的单词,然后在到达最后一个单词时停止循环。我当前的方法有点愚蠢…我在文档末尾插入一个无意义的单词,然后循环,直到它被选中并加上重音 也许有更好或更有效的方法来处理整个事情 Public Sub Button5_Clic

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

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

也许有更好或更有效的方法来处理整个事情

    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
Public子按钮5\u单击(发送者作为对象,e作为事件参数)处理按钮5。单击
将文档设置为Word.document
document=Globals.ThisAddIn.Application.ActiveDocument
Dim mySelection=document.Application.Selection
'确保光标位于文档的开头
document.Application.Selection.HomeKey(单位:=Microsoft.Office.Interop.Word.WdUnits.wdStory)
'在末尾插入假单词以停止循环
变暗范围为Word.range
range=document.range()
范围。插入符(“文档结束符”)
做
'使用MS Word的通配符选择第一个单独的单词作为修剪字符串
mySelection.Find.Text=“”
mySelection.Find.MatchWildcards=True
mySelection.Find.Execute()
'将已找到的选定单词替换为重音对应词
mySelection.Text=accentude.accentude.doAccentude(mySelection.Text)
循环直到mySelection.Text=“documentendatóris”
端接头

我不知道这是否更有效,但你可以使用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
document=Globals.ThisAddIn.Application.ActiveDocument
变暗范围为Word.range
范围=document.Content
将当前值设置为整数
电流=0
模糊的词语,如单词
单词=范围
模糊词作为词。范围
做
电流=电流+1
如果当前<字数,则计数
单词=单词(当前)
如果word.Text.EndsWith(“”),则
word.Text=word.Text.Trim()+“'s”
'将已找到的选定单词替换为重音对应词
'mySelection.Text=强调.强调.不强调(mySelection.Text)
其他的
word.Text=word.Text.Trim()+“'s”
如果结束
如果结束
循环直到当前=字。计数

这是否需要支持文档中的所有故事范围?目前,这在页脚、脚注和尾注中不起作用;它只在主文本故事中起作用。@jJack,它只需要在主文本中起作用。谢谢我认为“更有效”是在用户体验方面,因为之前的解决方案是让用户显式地选择要强调的每个单词。你的解决方案看起来不错。@johoso既然你不接受tinamou的答案,你能解释一下差距吗?