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

  • https://secure.gravatar.com/avatar/13bfdc7b99595dacf9615d5f9f39f067?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Stephen says:

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

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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.

  • https://secure.gravatar.com/avatar/02635132b317774e9b470399b2d0f7da?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Anonymous says:

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

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dmitry Kostochko (Add-in Express Team) says:

    Hi,

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

  • https://secure.gravatar.com/avatar/d254aa9323af53c88717a2a82bbfae76?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Mohamed Al-ashraf says:

    The function replace is not defined!

  • https://secure.gravatar.com/avatar/29957f26ad2d8ba527fd9cc8cfa7b2e0?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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?

  • https://secure.gravatar.com/avatar/c842efba6643e227fedc98c09521faf9?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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

  • https://secure.gravatar.com/avatar/29957f26ad2d8ba527fd9cc8cfa7b2e0?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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!