PDA

View Full Version : Pop up when clicking a link



austenr
03-14-2006, 01:37 PM
I need some code to generate a pop up box when you click a link. Just a simple pop up with a message thats all. Thanks

Killian
03-16-2006, 02:34 AM
The hyperlink object doesn't have any events so I don't see a way to do this. :dunno

austenr
03-16-2006, 06:59 AM
hi K,

Is there a way to have a roll over or hover over like in JavaScript? Or does word not support that either?

Killian
03-16-2006, 07:38 AM
Well in JS on a webpage, you are actually using events that are part of the DOM (document object model) as interpreted and detected by the browser.

And now you've made me think about it properly, I realise that not only is it possible, it's very easy! :doh:

You need to create a class that has an object variable declared as a Word app with events.
The class code can then use the selection change event to check if the selection is inside the range of a hyperlink.

In a standard module you'll need to declare and create an instance of the class before you'll get access to those application events

Code for standard module:Dim myApp As New cAppEventClass

'run this first to enable app events
Sub RegisterEventHandler()
Set myApp.appWord = Word.Application
End Sub
Code for Class module (named "cAppEventClass")Public WithEvents appWord As Word.Application

Private Sub appWord_WindowSelectionChange _
(ByVal sel As Selection)

If IsSelectionInHyperlink(sel) Then MsgBox "You're in a hyperlink"

End Sub

Private Function IsSelectionInHyperlink(sel As Selection) As Boolean
Dim h As Hyperlink
For Each h In ActiveDocument.Hyperlinks
If sel.InRange(h.Range) Then
IsSelectionInHyperlink = True
Exit For
End If
Next
End Function

fumei
03-16-2006, 06:10 PM
Nice one Killian. Although of course it is not really a pop up. You have put the cursor IN the hyperlink, move the mouse to click the OK button on the messagebox (not pop up), then move the mouse back to the hyperlink to actually use the hyperlink.

Killian
03-17-2006, 06:42 AM
Thanks Gerry, and what you say is quite right. We don't get the same events on a word document that we have available in a browser and the window_click events don't help us either so I think SelectionChange is about as close as we'll get.

In actual use, if the user grabs the mouse and clicks on a link, we can show a message box (or whatever) and all's well.
We are left with some other behaviour that isn't wanted, i.e. if the user just moves the selection into a hyperlink in any another way, like scrolling through the document, they'll still get the message. This could be extremely annoying but unless we can find a way of using a suitable mouse event (that way we could test for a left button press) we're kinda stumped.

One possible way to filter out the stuff we don't want could be to use the WinAPI to set a hook into the Word app and monitor the keyboard events. that way we could test if the selection is in a hyperlink AND the Ctrl key is down (assuming the user will always follow links by ctrl-clicking them.

Sounds great, but the selection change event is going to be firing quite a lot (!) and we'd have to be sure we got it right :yes

Worth pursuing?

fumei
03-17-2006, 08:26 AM
Oh......I kinda doubt that very much. Although as I don't know what the intention was here, it is hard to say. It does not seem worth pursuing to me, but hey...I did not post this.