【Word VBA】ユーザー辞書へ用語を一括登録するWordマクロ

2014年10月4日

翻訳をしているときに専門用語に赤い波線が引かれることがあります。

ユーザー辞書 に登録していない言葉には、スペルミスの表示である「赤線」が自動で引かれるのです。

かといって数百もあるような専門用語を1つ1つ登録して赤線がなくなるようにするのは非常に手間がかかります。

そこで、用語を一括で登録する方法を紹介します。

10月11日のSFAのセミナー の受講予定の方からのリクエストがあり作成しました)

このマクロでできること

現在開かれているファイルに記載されている用語を、ユーザー辞書にすべて登録します。

専門用語を登録すれば、今後、赤い波線が引かれることはありません。

マクロ1の解説

ファイルシステムオブジェクトを用いてファイルを開き、書き込みます。

マクロ1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Sub ユーザー辞書への一括追加()
 
 Dim myCustmDic As Object
 Dim myDicPath As String
 Dim fs As Object
 Dim myDicFile As Object
 Dim myPara As Paragraph
 
 '文書が開かれていない場合には終了
 If Documents.Count = 0 Then Exit Sub
 
 If MsgBox("ユーザー辞書登録を実行しますか?", vbYesNo) = vbNo Then Exit Sub
 
 Set myCustmDic = Application.CustomDictionaries.ActiveCustomDictionary
 
 'ユーザー辞書のファイルパスの取得
 myDicPath = myCustmDic.Path & Application.PathSeparator & myCustmDic.Name
  
 '辞書を書き込める形式(ForAppending(8))で開く FormatはUnicode(-1)に設定
 Set fs = CreateObject("Scripting.FileSystemObject")
 Set myDicFile = fs.OpenTextFile(FileName:=myDicPath, iomode:=8, Format:=-1)
 
 '現在開かれている文書の段落ごとに登録
 For Each myPara In ActiveDocument.Paragraphs
  If Trim(myPara.Range.Text) <> vbCr Then
   myDicFile.WriteLine Trim(myPara.Range.Text)
  End If
 Next
 
 'ユーザー辞書を閉じる
 myDicFile.Close
 
 'オブジェクト変数の解放
 Set myCustmDic = Nothing
 Set myDicFile = Nothing
 Set fs = Nothing
 
End Sub

マクロ2の解説

上記マクロ1と同じような仕組みで、ユーザー辞書のファイルパスを取得します。

そのファイルをWordメモ帳でそのまま開いてしまうというのが、このマクロ2の趣旨です。

ここに追記していけばいいのですから、簡単です。

なお、書式がついた用語であっても書式は保存されません。

マクロ2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Sub ユーザー辞書を開く()
 
 Dim myCustmDic As Object
 Dim myDicPath As String
 
 If MsgBox("ユーザー辞書登録を開きますか?", vbYesNo) = vbNo Then Exit Sub
 
 Set myCustmDic = Application.CustomDictionaries.ActiveCustomDictionary
 
 'ユーザー辞書のファイルパスの取得
 myDicPath = myCustmDic.Path & Application.PathSeparator & myCustmDic.Name
 
 'ユーザー辞書を開く
 Shell "notepad.exe " & myDicPath, vbNormalFocus
 
 'オブジェクト変数の解放
 Set myCustmDic = Nothing
 
End Sub

-コード
-,

S