In one of my previous HowTo samples I showed you how to use the Microsoft Word Object Model capabilities for spell checking. Today we will look at one more ability of MS Word, namely the capabilities of its dictionary, more specifically synonyms and antonyms. We will need the SynonymInfo object and SynonymList and AntonymList properties.
Using the code bellow, we can promptly and easily get a list of synonyms for a given word, i.e. a word on which the cursor is placed:
Private Sub AdxCommandBarButton1_Click( _
ByVal sender As System.Object) _
Handles AdxCommandBarButton1.Click, ShortcutSynonym.Action
Dim frm As New Form1()
frm.ListBox1.Items.Clear()
frm.Text = "Synonym list"
frm.Label1.Text = "Select synonym:"
Dim selection As Word.Selection = WordApp().Selection
If selection IsNot Nothing Then
Try
Dim wordText As String = selection.Words.Last.Text
Dim objLanguage As Object = Word.WdLanguageID.wdEnglishUS
Dim info As Word.SynonymInfo
info = WordApp().SynonymInfo(wordText, objLanguage)
If info IsNot Nothing Then
Try
If info.MeaningCount > 0 Then
Dim duplicate As Boolean
For Each meaning As String In _
DirectCast(info.MeaningList, System.Array)
Dim objMeaning As Object = meaning
Dim synonyms As System.Array
synonyms = DirectCast( _
info.SynonymList(objMeaning), _
System.Array)
For Each synonym As String In synonyms
duplicate = False
Dim list As String
For Each list In frm.ListBox1.Items
If list = synonym Then
duplicate = True
Exit For
End If
Next
If Not duplicate Then
frm.ListBox1.Items.Add(synonym)
End If
Next
Next
frm.ListBox1.SelectedIndex = 0
If frm.ShowDialog() = DialogResult.OK Then
ReplaceWord( _
selection, _
wordText.Trim().Length, _
frm.ListBox1.Text)
End If
Else
MessageBox.Show( _
"There are no synonyms for this word.")
End If
Finally
Marshal.ReleaseComObject(info)
End Try
End If
Finally
Marshal.ReleaseComObject(selection)
End Try
End If
frm.Dispose()
End Sub
The code that allows getting the antonyms list is more compact and easier. You can get it by downloading the VB.NET and C# samples below.
Please keep in mind that you can get the synonym and antonym lists not only via a COM add-in but from a standalone application as well by using the MS Word Object Model capabilities.
You may also be interested in:
How to develop a COM add-in for Microsoft Word step-by-step
Available downloads:
This sample add-in was developed using
Add-in Express 2008 for Microsoft Office and .net
C# sample Word add-in for VS 2005
VB.NET sample Word add-in for VS 2005




