Thesaurus

The thesaurus for synonyms and antonyms is available in Microsoft Office applications.

To define the thesaurus, we can say that it is a category for documentary languages. The terms (as motorbike, plane, car…) are linked together by relations of synonymy, hierarchy and association. Each term belongs to a category or domain.

The thesaurus is often used to search equivalents word.

Search synonymous in VBA

To search synonyms of a word, you can use the SynonymInfo object. This object takes in parameter two arguments which are the word for which one seeks synonyms and the language in which one wishes the results.

Sub UseThesaurus()

    Dim siTemp As SynonymInfo
     
     'Search synonymous of authorize
    Set siTemp = SynonymInfo(Word:="authorize", _
                LanguageID:=wdEnglishUs)
    
    If siTemp.Found = True Then
        MsgBox "The thesaurus has suggestions."
    Else
        MsgBox "The thesaurus has no suggestions."
    End If

End Sub

Write synonymous in a sheet

To use the Word application thesaurus, first, you need to create a Word object. Once the object created, you can use the SynonymInfo function which give you all the synonyms of your word. You can save the list of words in an array and display this list in your sheet.

Sub WriteSynonymous()
'Use the Thesaurus given by Word Application
    
    Dim WdApp, Syn As Object
    
    'Create a Word Application
    Set WdApp = CreateObject("Word.Application")
    'Stock synonymous for the word "strong"
    Set Syn = WdApp.SynonymInfo(Word:="strong", _
        LanguageID:=wdEnglishUs)
    
    If Syn.MeaningCount >= 2 Then
        synList = Syn.SynonymList(2)
        For i = 1 To UBound(synList)
            'Print all synonymous in the sheet
            Cells(i, 1).Value2 = synList(i)
        Next i
    Else
        MsgBox "There is no second meaning for the selection."
    End If

End Sub

Test if synonym exists

If you are looking at the existence of synonyms for a word, you can create a Word application then use the Found method on the SynonymInfo object. The Found method returns a boolean indicating whether or not there are synonyms. as in the following macro :

Sub ExistSynonymous()
'Test if a synonymous exist thanks to the Thesaurus
'given by a Word application

    Dim WdApp, WordDoc, Syn As Object
    
    'Create a Word Application
    Set WdApp = CreateObject("Word.Application")
    Set WordDoc = WdApp.Documents.Add
    
    'Stock synonymous for the word "strong"
    Set Syn = WdApp.SynonymInfo(Word:="strong", _
        LanguageID:=wdEnglishUs)
        
    'Test if synonymous have been found
    If Syn.Found = True Then
        MsgBox "The thesaurus has suggestions."
    Else
        MsgBox "The thesaurus has no sugestions."
    End If

End Sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s