Consulting

Results 1 to 7 of 7

Thread: Pop up when clicking a link

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    Pop up when clicking a link

    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
    Peace of mind is found in some of the strangest places.

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    The hyperlink object doesn't have any events so I don't see a way to do this.
    K :-)

  3. #3
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    hi K,

    Is there a way to have a roll over or hover over like in JavaScript? Or does word not support that either?
    Peace of mind is found in some of the strangest places.

  4. #4
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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!

    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:[VBA]Dim myApp As New cAppEventClass

    'run this first to enable app events
    Sub RegisterEventHandler()
    Set myApp.appWord = Word.Application
    End Sub
    [/VBA]Code for Class module (named "cAppEventClass")[VBA]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[/VBA]
    K :-)

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.
    Last edited by fumei; 03-16-2006 at 06:29 PM.

  6. #6
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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

    Worth pursuing?
    K :-)

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •