2008-11-17 72 views
1

我需要檢查Word文檔中的各種代碼。Word VBA訪問自定義詞典的內容

這些代碼通常是一個字母后跟連字符和5位數字,例如M-81406。

我們需要檢查這些預定義的代碼是否正確輸入(有幾千個代碼的預定列表)。 我們不能使用普通的Word拼寫檢查,因爲您不能拼錯一個數字(至少需要2個字母)。

我想我可以寫VBA代碼,發現任何可能無效的代碼。我可以使用正則表達式來檢查它們。

我可以從VBA訪問自定義詞典的內容嗎?

我需要一個方便客戶更新的解決方案。

獎金指向任何人建議突出顯示不正確的代碼的聰明方式。

回答

1

自定義詞典,你可能知道,只是包含的話,那麼引用Microsoft腳本運行時的文本文件,並使用:

Dim FSO As New FileSystemObject, FS As TextStream 
Dim Code As String, Codes As New Scripting.Dictionary 
Dim Paragraph As Paragraph, Word As Range 

Set FS = FSO.OpenTextFile("c:\...\cust.dic") 

Do Until FS.AtEndOfStream 
    Code = FS.ReadLine 
    If Not Codes.Exists(Code) Then Codes.Add Key:=Code, Item:=Nothing 
Loop 

' Use your own method for enumerating words 
For Each Paragraph In ActiveDocument.Paragraphs 
    Set Word = Paragraph.Range 
    Word.MoveEnd WdUnits.wdCharacter, -1 

    If Not Codes.Exists(Word.Text) Then 
     Word.Font.Underline = wdUnderlineWavy 
     Word.Font.UnderlineColor = wdColorBlue 
    Else 
     Word.Font.Underline = wdUnderlineNone 
     Word.Font.UnderlineColor = wdColorAutomatic 
    End If 
Next 

並不理想,因爲它則會覆蓋下劃線格式,並沒有按不提供建議機制(儘管圍繞列表框構建的小表單就足夠了)。

不幸的是,最好的解決方案將涉及到擴展拼寫引擎。