【问题标题】:better method for accenting every word in Word document?在 Word 文档中强调每个单词的更好方法?
【发布时间】:2013-04-07 11:01:33
【问题描述】:

我是编程新手,但我正在尝试将现有脚本改编为 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

【问题讨论】:

  • 这是否需要支持文档中的所有故事范围?目前这不适用于页脚、脚注和尾注;它只适用于正文故事。
  • @jJack,它只需要在正文中工作。谢谢!

标签: vb.net ms-word office-interop


【解决方案1】:

好吧,我真的不知道它是否更有效,但您可以使用 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

【讨论】:

  • 我认为“更有效”是在用户体验方面,因为之前的解决方案是让用户明确选择每个单词来强调。您的解决方案看起来不错。
  • @johhoso 既然你不接受 tinamou 的回答,你能解释一下差距吗?
猜你喜欢
  • 2020-03-15
  • 2013-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
  • 2013-12-23
相关资源
最近更新 更多