2013-04-07 32 views
2

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

脚本“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 
+0

这是否需要支持文档中的所有故事范围?目前,这在脚注,脚注和尾注中不起作用;它只会在主要的文字故事中起作用。 – JohnZaj 2013-04-07 21:59:51

+0

@jJack,它只需要在正文中工作。谢谢! – johhoso 2013-04-08 08:41:31

回答

2

好了,我不知道真的如果它的更有效的方法,但你可以使用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 
+0

我认为'用户体验'更有效率,因为之前的解决方案是让用户明确地选择每个单词来突出显示。你的解决方案看起来不错 – JohnZaj 2013-04-11 04:35:49

+0

@johhoso,因为你不接受tinamou的答案,你能解释这个差距吗? – JohnZaj 2013-04-12 03:03:40

+0

@tinamou,对于延迟抱歉。感谢您的回答! – johhoso 2013-04-12 05:31:57

相关问题