2015-07-21 104 views
0

我正在寫一個用於在Word文檔中定義首字母縮略詞的模塊。該腳本從Excel文檔中獲取首字母縮略詞和定義。我遇到的麻煩是將首字母縮略詞的第一個實例的位置與完整定義的第一個實例的位置進行比較。 最終,我需要確保首字母縮略詞的第一個實例緊接在定義的第一個實例後面,並用圓括號括起來。完成後,腳本需要刪除定義的後續實例,因此我還需要弄清楚如何刪除除定義的第一個實例以外的所有實例。VBA在Word文檔中比較兩個單詞的位置

最終的結果應該是這個樣子:

....這份文件是關於軟件即服務(SaaS)。 SaaS是由其他人託管的軟件。您可以通過Web瀏覽器訪問它,而不是將其安裝在您自己的計算機上。有很多類型的SaaS。 ....

我怎樣才能得到這兩個元素的位置和或比較它們的位置?

在上面的例子中,我將如何找到「SaaS」的第一個實例並確保它在(空格,左括號)定義(假定定義實際出現在文檔中)之後的兩個位置出現?

'Selects first instance of acronym. Get start and end positions of first instance of acronym. 
    Selection.HomeKey Unit:=wdStory 
    Selection.Find.Execute Acronym 'Acronym is a variable. Now that it's selected, I need to get it's start position (or the position of the cursor if the cursor is at the start of the acronym) or find a way to compare it's position to the UserSelection variable. 

    'Is the definition in the document? 

     'If no, add definition before first instance of acronym. 

     'If yes, get start and end positions of first instance of definition. 

    'Is end position of first instance of definition adjacent to start position of first instance of acronym? If not, which is first? 

     'If definition is first, add acronym behind definition. 

     'If acronym is first, add definition in front of acronym and delete remaining instances of definition. 

    'Highlights all instances of the acronym in green 
     With ActiveDocument.Content.Find 
      .MatchCase = True 
      .MatchWholeWord = False 
      With .Replacement 
       .Highlight = True 
      End With 
      .Execute Replace:=wdReplaceAll, Wrap:=wdFindContinue, FindText:=Acronym, ReplaceWith:=Acronym 
     End With 

任何幫助或洞察力將不勝感激,因爲我完全處於虧損狀態,沒有Google的運氣。

-Vince

回答

0

我認爲下面的代碼片段可以幫助你:

Sub example(acronym, definition) 

    Selection.Find.ClearFormatting 
    With Selection.Find 
     .Replacement.Text = "" 
     .Forward = True 
     .Wrap = wdFindStop 
     .Format = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchAllWordForms = False 
    End With 

    ActiveDocument.Range(0, 0).Select  ' go to beginning of document 
    Selection.Find.Text = acronym 
    Selection.MatchSoundsLike = False 
    If (Not Selection.Find.Execute) Then 
     ' acronym not found in this document 
     Exit Sub 
    End If 

    ActiveDocument.Range(0, 0).Select  ' go to beginning of document 
    Selection.Find.Text = definition 
    Selection.MatchCase = False 
    Selection.MatchSoundsLike = True 

    While (Selection.Find.Execute) 
     ' 
     Selection.Delete     ' delete all definitions 
     ' 
    Wend 

    ActiveDocument.Range(0, 0).Select  ' go to beginning of document 
    Selection.Find.Text = acronym 
    Selection.MatchSoundsLike = False 
    If (Selection.Find.Execute) Then 
     Selection.InsertBefore "(" & definition & ")" 
    End If 

End Sub 

注:我還發現,作者提出在定義錯誤(微小變化),甚至是意想不到的額外空間混亂找到。

+0

Thanks @Paul Ogilvie。很有幫助。我同意作者作出微小的變化,但這將大大幫助我們的編輯確保首字母縮寫詞得到恰當的定義,特別是考慮到絕大多數縮寫詞不是由我們的文檔作者定義的。運行這個程序,然後花5分鐘清理跟蹤變化,比花2-4小時定義縮略詞要好得多。 –