PDA

View Full Version : Solved: Detecting a Mouse Click



Bilby
07-05-2006, 09:06 PM
Greetings All

I want to collect information about a paragraph and display it on a userform when the user clicks inside a paragraph. The form and collecting the information is easy, but how do you detect a mouse click in a Word Document?

fumei
07-05-2006, 09:26 PM
You can't, natively. You will have to use API.

But why bother? First of all how are you going to tell the difference between the user simply putting the cursor into a paragraph..say..oh, I don't know...to type something perhaps? - and when you want that putting the cursor into a paragraph to fire ANOTHER Sub that will load the Userform?

If you have the code for the form, and the information, why not:

a) have a hotkey fire the Userform.Show instruction; OR
b) have a button on a toolbar that will do the same?

The point is, if you have something that will load the userform when the user clicks on a paragraph...then it will do that every time the user clicks in a paragraph. Which....hmmmm...the user may not want ALL the time.

Letting them CHOOSE to see the information seems much more friendly.

Bilby
07-05-2006, 10:03 PM
Naaaa,

What I want to do is have a modaless form open and display allowable options based on where/what the user does. Got Toolbars, Got Buttons, but I got no users that know what they're doing.

I can do it by triggering macros but it would be neat if just clicking in a paragraph brought up the relevant info.

As I've never used an API from VBA can you point me in the direction of any examples/sites?

fumei
07-08-2006, 12:38 AM
I can do it by triggering macros but it would be neat if just clicking in a paragraph brought up the relevant info.Yes, I suppose. Just remember that you are not just needing to detect a click, but a click IN a paragraph. What if they click the...oh, I don't know...the Save button. It is a click. How are you going to tell if that click needs to fire your info procedure...or not?

Can be done I suppose. Actually I don't doubt it can be done, but unless you are really wanting to do the work....I fail to see the gain for the effort.

There are lots of API sites. Start googling, and good luck.

Bilby
07-09-2006, 02:31 PM
Thanks for your input, It would have been nice if there was some WOrd event that I could have used but I guess i'll just have to google.

fumei
07-10-2006, 10:51 PM
No, sorry there is no Word event.

Killian
07-11-2006, 05:25 AM
While Word doesn't give you an appropriate document event, you can create an application class with events enabled and use the "WindowSelectionChange" event to grab the paragraph text and put it in a form.
I've attached a basic example.

fumei
07-11-2006, 06:03 AM
Yes, making a class will do it. Thanks for that K.

fumei
07-11-2006, 07:00 AM
Just to add to K's excellent starting...here is further possible enhancement.

The attached file has the firing routine as a clickable "Test macro" icon on the Menu bar.

Clicking it will fire the userform. You then can go in the document and put the Selection where you want, and information will be displayed on the userform.

Clicking the "Test macro" again, will hide the userform. It is still running. You can move the Selection anywhere. Clicking the "Test macro" again will display the userform with the current values. This way, if the user wants, the userform can be hidden away, out of the way, but will immediately give the current values for the current selection location with a single click.

BTW: displayed information on a userform really should be in a Label, not a textbox. Textboxes are meant for user INPUT. If there is no user input, why have a textbox?

PS. the paragraph numbers are using a SEQ field for numbering.

MOS MASTER
07-11-2006, 08:50 AM
Hi Guys, :hi:

Here's some extra background on Applications events in Word:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnword2k2/html/odc_wdappevnt.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnword2k2/html/odc_wdappevnt.asp)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbawd11/html/wohowApplicationEvents1_HV05214014.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbawd11/html/wohowApplicationEvents1_HV05214014.asp)

IMO the WindowBeforeDoubleClick could be helpfull.

HTH. :whistle:

geekgirlau
07-11-2006, 04:43 PM
The other option might be to add it to the shortcut menu, so that the user can select the macro when they right-click the paragraph. Personally I'm with Gerry - having a user form pop up every time I want to click (or double-click) in a document would drive me insane.

fumei
07-11-2006, 08:35 PM
That is why I put it with the ability to hide it. When hidden, it is still running. They just have to click the macro button and it will give the CURRENT value of where the selection is.

fumei
07-11-2006, 08:38 PM
And I have to add something. While Killian's code works, technically it does NOT detect the mouse click. The mouse click is NOT detected. To repeat that, you can not detect the mouse click with application events in Word. You need to use API.

Killian's code detects a change in Selection.

To truly detect a mouse click...you need API.

MOS MASTER
07-12-2006, 01:55 PM
To repeat that, you can not detect the mouse click with application events in Word. You need to use API.
To truly detect a mouse click...you need API.
Hi Buddy, :hi:

Not so WindowBeforeDoubleClick is a mouse click eventhandler and WindowBeforeRightClick is too. They even get fired before the handler returns to the default click behaviour for that action.

Of course API's are great on the matter too....:whistle:

fumei
07-13-2006, 10:34 AM
Except the request was for A click...not a double click, not a right click. A click.

MOS MASTER
07-13-2006, 10:55 AM
Except the request was for A click...not a double click, not a right click. A click.

Ha, ha.. That I agree on buddy! :friends:

Killian
07-14-2006, 03:25 AM
Well, I also agree but I felt that perhaps the OP had jumped to the mouse-click conclusion - perhaps without considering the delights of selectionchange..?
Obviously, I could be completely wrong about that and the text on the form should NOT update if the user navigates the document with the keyboard and ONLY when the doc is clicked.
That would be strange but could be true... :dunno

fumei
07-14-2006, 04:46 AM
K....gee, I never thought of that. But there you go...it is only by fully grasping "requirements" can we do anything...uh....hmmmmmm...."right".

The only issue I have with SelectionChange is that if you move the selection down with the keyboard - oh, say 9 lines - to get to the next paragraph (assuming the current one is 8 lines down!) the code fires 9 times. For each Change. I dunno.

Killian
07-14-2006, 08:01 AM
The only issue I have with SelectionChange is that if you move the selection down with the keyboard - oh, say 9 lines - to get to the next paragraph (assuming the current one is 8 lines down!) the code fires 9 times. For each Change. I dunno.This is a very good point and I share your reservations.
Whenever working with events like SelectionChange, it's worth spening some time working out the quickest evalualtion method to test before you actually fire unnessecary code. In this case it might be better to store the current paragraph index (the one that's displayed on the form) in a variable and in the event, test to see if the new para index <> the stored one - that way the bulk of the code will effectively be firing on a "ParagraphChange" event.

MOS MASTER
07-14-2006, 08:22 AM
I Think the OP had more of its share of usable solutions.

But if I where the user I'd like to fire things up myself .. I would go completely insane if something would get fired solutions I clicked somewhere...:bug:
</IMG>

fumei
07-14-2006, 04:18 PM
Again, that is why I put it up on the toolbar, and if desired, it can run in the background, but only "activate" if the user wants it.