PDA

View Full Version : Get Word from MSWord



nazlan
03-15-2009, 11:26 PM
How can one get word(s) from MSWord on mouse right-click in VBA?

Searching MSDN, solution to get first word in MSWord


Sub SelectFirstWord()
ActiveDocument.Word(1).Select
End Sub

What is the way to get words after mouse right-click? ie get 1st word, 2nd word, 3rd word......and so on

:dunno
Thank you.

fumei
03-16-2009, 09:17 AM
If I right-click, I do not get a word selected. What are you doing to get this from a right-click?

nazlan
03-16-2009, 09:32 AM
Sorry.
That is what I am asking help for, ie right-click to get the word.
ActiveDocument.Words(1).Select will just give me the first word after running the macro. This was just my trial and error in trying to get words from MSWord.
Searching through google it was suggested to use Sel.Words(1) to get the text, but I have no idea how to do it in VBA. Help.

fumei
03-16-2009, 09:35 AM
You have to program the right-click. Try searching for doing that. Never mind getting a word, that is easy. In other words...

"That is what I am asking help for, ie right-click to get the word."

Try searching for getting a right-click to do anything.

What exactly are you trying to do, and why?

nazlan
03-16-2009, 10:46 AM
Thank you, I will try that.

'What exactly are you trying to do, and why?

I am trying to get at the words after a mouse right-click in order to find spelling suggestions (using getspellingsuggestions). MS Word only provide limited lists ( I think up to 5 suggestions).

Is there a VBA code to easily get words that are squiggly underlined in MS Word?

Thank you.

nazlan
03-17-2009, 07:42 AM
Fumei,
I think I manage to get the mouse right-click going without any duplication of menu items after restarting Word.
Please comment on the code which are based on examples after searching the net.
Another person was also asking about the same thing for mouse right-click in this forum.


Sub AutoExec()
Call AddMenuNazlan
End Sub

Sub AddMenuNazlan()
Dim cb As CommandBar
'remove duplicate in menu addition
RemoveMenu
'mouse right-click menu on shortcut menu under text for spelling options
Set cb = Application.CommandBars("Spelling")
Set c = cb.Controls.Add
With c
.Caption = "Nazlan Caption to do something"
.FaceId = 172
.Style = msoButtonIconAndCaption
'.OnAction = "I think this is where I run my codes"
.BeginGroup = True
End With
End Sub

Sub RemoveMenu()
On Error Resume Next
Application.CommandBars("Spelling").Controls("Nazlan Caption to do something").Delete
On Error GoTo 0
End Sub

' You have to program the right-click. Try searching for doing that. Never mind getting a word, that is easy. In other words...

Can you Fumei or anybody help in getting the words or text that we typed in MS Word, ie the words after mouse right-click on the squiggly lines!!!!

Thank you.

nazlan
03-20-2009, 01:06 AM
Anybody can help in getting the word after right-click on any of the characters in MS Word?

nazlan
03-23-2009, 07:04 AM
I can get the word using Selection.Words(1) but have to run the macro each time for the selection to work.
Is there a way to just load the document and the right-click will select the words on right click on the whole document with red squiggly lines without rerunning the macro?
Do not have any idea to proceed!!!

fumei
03-23-2009, 10:36 AM
I do not know how to proceed to suggest anything, as I really do not know what you are asking. I have no idea at all what you mean by:

"the right-click will select the words on right click on the whole document "

A right click will select the words on right click?

Huh?

Try it from the beginning.

You have a document.

There are a number of (I assume) spelling errors, so there are a number of words with red squiggly lines.

Are you asking that you can right click on ONE of those words, get the context menu, pick one of the items on that context menu, and then right-clicking the same word again...get the NEXT word with red squiggly?

If this is what you are asking....try actually asking that.

I do not know what you are really trying to ask.

nazlan
03-23-2009, 04:26 PM
Sorry, maybe I was not clear.

I was just trying to ask the following:-
Right click on ONE of those words (red squiggly ones) one can GET the words (for example abcd and cursor either between ab, bc, cd... will get word abcd). The word abcd will then be used in a subroutine.
After that I would like to move on to the next red squiggly line (another word) and get this word.....
Hope this helps.

fumei
03-24-2009, 11:46 AM
"I would like to move on to the next red squiggly line "

That is what I am trying to find out. HOW do you imagine moving on to the next word? WHAT is the logic that would move it on?

Please try and describe EXACTLY what you are thinking will happen.

"The word abcd will then be used in a subroutine.
After that I would like to move on"

is simply too vague.

Write it out, everything, step-by-step.

1. right click a word
2. select an item from list
3. that item "used in a subroutine"....how? If the selected item is passed to a subroutine, then THAT subroutine has focus and is being executed. WHAT happens when it finishes and returns execution to the starting Sub?
4. what if you do not want ANY of them...now what?
5. does the selected item acually get used (replacing the red squiggly word)?

geekgirlau
03-24-2009, 09:05 PM
Hi Nazlan,

What Gerry is describing is the first step in any development, which happens before you start writing a single line of code.

What do you want to happen? Don't get hung up on how you might achieve this (because there may be more than one way to go about it). Start by writing down the starting point (for example, a document containing spelling mistakes) and the end result that you are trying to reach (such as provide lots of alternative spelling rather than Microsoft's 5 choices). Be sure to include any information that might affect how you approach the task (for example, the users have limited skills, must be able to run quickly on a 100 page document etc.).

fumei
03-25-2009, 11:33 AM
"What Gerry is describing is the first step in any development, which happens before you start writing a single line of code."

Oh I wish.

geekgirlau
03-25-2009, 04:58 PM
Well yes Gerry, in an ideal world, and if you believe this I have a bridge I'd like to sell you :muahaha:

Let me rephrase: what we should always do before writing code ...

nazlan
03-28-2009, 11:18 PM
Hi,
I thought that was what I was trying to do, ie get the text with squiggly line first. After that try to get the spelling suggestions for the squiggly line text and try to differentiate suggestions coming from main dictionary and custom dictionaries (this forum showed me the way to differentiate the suggestions).
I am still stuck with the first problem... getting the text with squiggly lines, I have to run the macro each time to get the "word". I am wondering how one can just right click on the text and just grab the text!!! Not possible?

fumei
03-30-2009, 09:46 AM
I give up. I have been trying to help. I have asked you to describe exactly - and I mean exactly - what you are trying to do. You do not answer.

I have a document. The text in the document is:

Asdk and the ljalsd is before the hadl hadhad.

The bolded words have red squiggly lines under them. I can write code that displays a messagebox for each red squiggly word, with the list of suggestions for each one. So that I get a messagebox:

Red Squiggly Word: Asdk

Ask
Ads
Asks
Sad
ADSL
Sack

then...

Red Squiggly Word: ljalsd

last
lapsed
ladled
lased
laps
lass
jails
laws

then...

Red Squiggly Word: hadl

had
held
hall
haul
hail

then...

Red Squiggly Word: hadhad

had had
hashed
hardhead
hardhat

Therefore, my code goes through each word, if the word has SpellingSuggestions (i.e. it has a red squiggly line), then the word is displayed, along with a list of the suggestions.

I could get the word out that code, after all I have to have the word in order to put in it in the string:

Red Squiggly Word: word

However, this - I think - is not what you are asking. I am still not sure what you are asking. I know this must be frustrating for you, and I really would like to help, but I do not see how I can. From your other posts, you have the basis for the code you seem to want. Use it.

Good luck.

nazlan
03-30-2009, 06:14 PM
Hi Gerry,
Actually I am at the verge of giving up and my english is not up to par. I am using Office 2000 and 2003 and 2007, all english versions.
Word only gives 5 suggestions to the red squiggly word and I have an extended custom dictionaries.
Basically this is what I want to do if possible.

1. I want to do a check spelling as you type in MS Word 2000/2003. (While typing you get red squiggly lines if the words are deem wrongly spelled or are not in the main dictionary or custom dictionaries as in MS Word).
2. User has the option to right click immediately after a space typed and the red squiggly line comes up or later right clicks on every red squiggly lines after finishing typing the whole text).
3. On right click I (wish that this is possible) can get the word typed (red squiggly line) and get spelling suggestions (more than the usual 5 if possible) from main dictionaries and custom dictionaries, or the usual no spelling suggestions message on the context menu.
4. If there suggestions, the suggestion lists can be differentiated to come either from main or custom dictionaries (custom dict - xxxxx and main dict - yyyyy). I noticed that the custom dictionaries entries comes up first followed by the one from main dictionary. My previous post using replace gives only the custom dictionary suggestions which is also fine with me following your suggestions. I am quite satisfied with that.
5. The user can then choose to use the suggestions to replace the red squiggly word in MS Word or ignore it by clicking another red squiggly line. I think I have to do a before right-click to implement this and then show the right click event to get the ContextMenu. If ContextMenu cannot be customized then I want to do a pop-up of the suggestions with options to ignore or replace the red squiggly line.
6. I was hoping to do all this interactively in Word through VBA. Grammar checking will be off during the whole process and only "Check spelling as you typed" chosen.

Please do not give up to help....
Thank you for comments, directions and suggestions. I have VBA textbooks available but they are not of much help to what I am trying to do. Thank you.