PDA

View Full Version : Retrieving a list from the thesaurus



Brandtrock
02-08-2007, 09:13 AM
Is it possible to get the list of synonyms for a given word returned to the document (or a userform)?

I can get a list of synonyms from the thesaurus manually. This works well when the objective is to find a better or different word when writing a document. I tried the typical "start the macro recorder and get the code" routine, but this is one of those unrecordable things. All I got for code was the cursor movements used to select the word in question.

I am more "at home" in Excel, but have had no luck in that app either. I'm betting that Word users are more frequently using the thesaurus than Excel users, hence my asking in this forum.

Any help is greatly appreciated.

Regards,

fumei
02-09-2007, 09:25 AM
Yes, it is quite possible.

If you looked this up in Help you would have found the information. You can get a list of the meanings AND the list of each synonym for each meaning.

It is a little tricky, but not difficult. The examples in Help are fairly good. Give it a try and post back if you have difficulties.

fumei
02-09-2007, 09:42 AM
I have to ask though....what are you going to do with it? You can get the meanings and synonyms as strings, but then what?

What does "returned to the document" mean?

Say the word "help".

It has four meanings: assist, make easier, avoid, assitance. Each one has its own list of synonyms.

assist: aid, lend a hand, help out, facilitate, rally round, be of assistance

make easier: relieve, improve, ameliorate, alleviate

avoid: evade, stop, refrain from, prevent

assistance: aid, benefit, support, service, relief, comfort, advantage, good thing

So, FOUR meanings, and a total of 22 synonyms.

What are you going to do with this????? What does "returned to the document" mean?

Brandtrock
02-10-2007, 12:02 AM
My humblest apologies Gerry, I had both Excel and Word open, each with their VBE's open. I DID search for thesaurus in the VBA Help, unfortunately I did it in my Excel VBE. This yielded no results. I thought I was in my Word VBE when I searched. I surmised that if Word had no Thesaurus help, Excel wouldn't either. My mistake completely. Thanks for the tip, I did find it this time and will play with it to see if I can get it to do what I want.

Which brings me to what I want. I said "return to the document" meaning I wish to have all unique synonyms returned in a list in the word document I am in. This will be used for a homework exercise application that I am working on for my fifth grade son. The idea being that a given word; for example - help, as previously indicated, has 4 meanings and 22 synonyms, 21 of which are unique.

So, I enter Help and run a macro and it returns:

aid, lend a hand, help out, facilitate, rally round, be of assistance, relieve, improve, ameliorate, alleviate, evade, stop, refrain from, prevent, benefit, support, service, relief, comfort, advantage, good thing

I have used the Thesaurus in Excel to "manually" fill 25 rows with a word in one column and all of the synonyms in the next column. Counting the length of the string less the length of the string with commas replaced by "" and adding 1, gives me the number of synonyms for the word. The student selects a word from my list via dropdown on a userform and then enters a synonym guess. If it is in the string, they get a point and continue, if not, the next student goes.

Okay, so it is probably a nerdy game idea, but the teacher I am working with liked it. Anyhow, I ran into walls in Excel as the Thesaurus is not a highly vaunted feature in that application. I figured I could use Word from Excel once I figured out how to do it in Word.

I hope that clears up why my odd little request was made. I'll post back if, perhaps I should say when, I get stuck getting Word to do what I want it to do, (not what I tell it to do).

Regards,

fumei
02-10-2007, 06:01 PM
OK, you explained what you did in Excel.

You did not explain what "returned" means in Word.

You run "help", you can GET all those words, but - again - WHAT are you going to do with them?

Put them all into the document as text? That dopes not seem reasonable. So...I am stumped. WHAT do you do with those words?
meaning I wish to have all unique synonyms returned in a list in the word document I am in. You are going to create a list IN the document?

Brandtrock
02-10-2007, 07:50 PM
Sorry if I wasn't clear enough. The end goal will be to get all of the unique synonyms in a list in a cell in Excel. If I don't have to use Word to do that, fine, but as I said I was not having any joy getting it done in Excel.

I went to word to see if I could get the words in a list in a Word document. Unfortunately for me, I have not even tried this yet as the "honey do" list was too long to let me play with the computer today (until now). When the list of terms is gathered, it can go straight to Excel as far as I am concerned, returning to Word would be an extra (unneeded) step I imagine.

Pulling the lists programmatically was just a hope of mine as it will save me some time and thus give me more flexibility in adding words to the list in my application.

Has this made things more clear? I hope so, if not, I'll give it another whirl. I should be able to try some coding tonight as the boys are away and the wife is already in bed since 6:00 pm because she has to go in to work at 2:45 am Sunday morning.

Regards,

fumei
02-11-2007, 01:54 PM
Dim msg As String
Dim var
Dim i As Long
Dim mySi As SynonymInfo
Dim synList() As String

Selection.Expand Unit:=wdWord
Set mySi = Selection.Range.SynonymInfo
For var = 1 To mySi.MeaningCount
synList = mySi.SynonymList(Meaning:=var)
For i = 1 To UBound(synList)
' this could type the list at the end of the document
' Selection.EndKey Unit:=wdStory
' Selection.TypeParagraph
' Selection.TypeText Text:=synList(i)

' while THIS makes a string of the list
msg = msg & synList(i) & vbCrLf
Next i
NextThe code expands the selection to the word it is in, so the whole word does not have to be selected.

You could export each word over to Excel, one at a time as it is determined. That seems inefficient. Or you could parse the produced string of all of them, and put each word into Excel.

There is no logic here for determining "unique". You would have to build that. In which case, I would put the returned words from the list into an array. Apply the logic to the array.

mdmackillop
02-11-2007, 02:18 PM
Hi Gerry,
Just out of interest I tried to get a synonym list from a word supplied by an InputBox but couldn't get it working. Any ideas?
Regards
Malcolm

Brandtrock
02-11-2007, 07:17 PM
Gerry,

Thanks for the code. I'll try to get it integrated with my Excel code next and let you know how it turns out. Your code does exactly the type of thing I was looking for.

Malcolm,

This worked for me.

Sub FromInputBox()
Dim msg As String
Dim var
Dim i As Long
Dim mySi As SynonymInfo
Dim synList() As String
Dim MyReply As String

MyReply = InputBox("Enter a word to research: ", "Synonym List", "Help")
Set mySi = SynonymInfo(Word:=MyReply)
For var = 1 To mySi.MeaningCount
synList = mySi.SynonymList(Meaning:=var)
For i = 1 To UBound(synList)
' this could type the list at the end of the document
' Selection.EndKey Unit:=wdStory
' Selection.TypeParagraph
' Selection.TypeText Text:=synList(i)

' while THIS makes a string of the list
msg = msg & synList(i) & vbCrLf

Next i
Next
End Sub
Regards,

mdmackillop
02-12-2007, 05:59 AM
Thanks Brandtrock, that will come in very useful.(helpful/handy/convenient.....)

fumei
02-12-2007, 11:06 AM
Yes, SynonymInfo returns values from either a given Range, or a given string. Note that if given a Range, the difference between Range.Start and Range.End must be >= 1. In other words, it will return an error if Start = End.

A Selection.Range of just a cursor - no character selected, therefore Start = End - returns an error. That is why in my code example, I expanded the Selection to the word the Selection is in.