Dmitry Kostochko

HowTo: Use MS Word Object Model to get synonyms and antonyms lists programmatically

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

8 Comments

  • Stephen says:

    How to get all the words from the MS WORD main dictionary using VB 6.

  • Dmitry Kostochko (Add-in Express Team) says:

    Hello Stephen,

    As far as I know, the main Microsoft Word dictionary is not accessible. The Word Object Model doesn’t provide any objects/methods/properties for developers to access the main dictionary.

  • Anonymous says:

    Hello,
    we would like to know how to use the WordApp(), how to declare it, or witch type is it?

  • Dmitry Kostochko (Add-in Express Team) says:

    Hi,

    WordApp is a property of the AddinExpress.MSO.ADXAddinModule class. Its type is Word._Application.

  • Mohamed Al-ashraf says:

    The function replace is not defined!

  • Andrei Smolin (Add-in Express Team) says:

    Hello Mohamed,

    I’ve downloaded the C# version of the add-in and it works fine for me. What problem do you have?

  • Gan says:

    Hi
    It was said “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:”. My question is where to run this code?Inside a word document as a macro? or through a .NET application?
    I opened the solution in Visual Studio 2015. The solution is migrated to VS 2015 version and complains that file ‘AddinExpress.MSO.2005’ is missing? How can I download this file

    many thanks

  • Andrei Smolin (Add-in Express Team) says:

    Hello Gan,

    That project you’ve downloaded is an Add-in Express add-in project. You need to buy Add-in Express in order to compile the project. Alternatively, you can use that code as a base for writing a VBA macro of your own.

Post a comment

Have any questions? Ask us right now!