Printing out a complete list of nouns, adjectives, adverbs, or verbs
QUERY
I would like to know more about how the Synonym Info class works.
I had wanted to buy more books on Word VBA, but it would be too expensive.
Can anyone provide me with some examples?
SOLUTION
The SynonymInfo class is used to retrieve information about synonyms for a particular word or phrase. It has several properties and methods that allow you to get information about the synonyms, such as the number of synonyms, the synonyms themselves, and their part of speech.
Here’s an example of how to use the SynonymInfo class to get the synonyms for a particular word: Copy
Sub GetSynonyms()
Dim synInfo As SynonymInfo
Set synInfo = ActiveDocument.Range.SynonymInfo("happy", wdSynonymAntonym)
If synInfo.MeaningCount > 0 Then
Dim i As Integer
For i = 1 To synInfo.MeaningCount
Debug.Print "Meaning " & i & ":"
Dim j As Integer
For j = 1 To synInfo.MeaningList(i).Count
Debug.Print synInfo.MeaningList(i).Item(j)
Next j
Next i
Else
Debug.Print "No synonyms found."
End If
End Sub
In this example, we use the SynonymInfo
method to get the synonyms for the word “happy”. We specify the wdSynonymAntonym
constant as the second argument, which tells Word to return both synonyms and antonyms. We then check if there are any synonyms found and if so, we loop through the list of meanings and the list of synonyms for each meaning, and output them to the Immediate window.
Here’s another example that uses the SynonymList
property to get the synonyms for a word in a specific part of speech: Copy
Sub GetAdjectiveSynonyms()
Dim synInfo As SynonymInfo
Set synInfo = ActiveDocument.Range.SynonymInfo("happy", wdSynonymAdjective)
If synInfo.SynonymCount > 0 Then
Dim synList As Variant
synList = synInfo.SynonymList
Dim i As Integer
For i = LBound(synList) To UBound(synList)
Debug.Print synList(i)
Next i
Else
Debug.Print "No synonyms found."
End If
End Sub
In this example, we use the SynonymInfo
method to get the synonyms for the adjective form of the word “happy”. We specify the wdSynonymAdjective
constant as the second argument, which tells Word to only return synonyms that are adjectives. We then get the list of synonyms using the SynonymList
property and output them to the Immediate window.