2013-04-07 94 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