Results 1 to 8 of 8
  1. #1
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Form with a hyperlink (VBA/Office XP)

    Hi,

    I would like to add an "About" form to my Word template that has a button (or, more appropriately an image) which, when clicked takes the clicker to a web site. Can I do this with VBA? If so, how? I can't find a darned thing anywhere no matter how I search for it.

    If not, do I need VB?

    Thanks in advance,
    Kim

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Form with a hyperlink (VBA/Office XP)

    On a UserForm, you can attach a ControlName_Click() procedure to a text label, an image, a button, etc.

    To launch a web page, there are easier and harder ways, and more and less reliable ways. What I've done recently is (in part):

    <pre>'Create browser object references
    ' [Set a reference under Tools|References to Microsoft Internet Controls]
    Dim ie1 As New InternetExplorer

    With ie1
    .Visible = True 'show window
    .navigate strURL 'open page
    While Not .readyState = READYSTATE_COMPLETE
    Sleep 500 'wait 1/2 sec before trying again
    Wend
    End With</pre>

    Sleep is an API function that you have to declare at the top of your code module (not sure if you can do this in a UserForm code module; you might need to make it public in a regular module):

    <pre>'Declare Sleep API
    Private Declare Sub Sleep Lib "kernel32" (ByVal nMilliseconds As Long)</pre>

    Hope this helps get you started.

  3. #3
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Form with a hyperlink (VBA/Office XP)

    Dang, that's slick.

    And now that I know it's possible, I'm curious about the browser reference. Is there a way to access the default browser? Which might not be IE? It will undoubtedly be IE, but it would be good to know. Is that something that could be teased out of Windows?

    Thanks so much for your code. I knew I could attach a click to an image or button, but I couldn't find what to do with it. I'll post whether the reference can go in the form or needs to go in a public module.

    Kim

  4. #4
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Form with a hyperlink (VBA/Office XP)

    And the answer is: you can declare Sleep at the top of the UserForm. It worked just great! Now if there's a way to call the default browser...

    K

  5. #5
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Form with a hyperlink (VBA/Office XP)

    I would have done it this way
    <pre>Private Sub Image1_Click()
    Application.ActiveDocument.FollowHyperlink Address:="http://www.Microsoft.com", _
    NewWindow:=True, AddHistory:=False
    End Sub</pre>

    This should allow the default browser (or local application if not going to a html file) to get the gig.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Form with a hyperlink (VBA/Office XP)

    If you don't need to manipulate the browser window, it looks as though it makes much more sense to use Andrew's method.

  7. #7
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Form with a hyperlink (VBA/Office XP)

    See this This thread for another approach using the ShellExecute API call. It will launch the default browser (or other appropriate application in the case of non web documents). This method should work for all flavours of VBA and VB.

    If you are using an Image control, youi can set the Tag property to the appropriate URL.

    Andrew C

  8. #8
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Form with a hyperlink (VBA/Office XP)

    Thanks to all of you for the input. I'll try them all and see which works best for my situation, but I'm very grateful for the info.

    K

Posting Permissions

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