• Form with a hyperlink (VBA/Office XP)

    Author
    Topic
    #384054

    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

    Viewing 0 reply threads
    Author
    Replies
    • #657281

      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):

      '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

      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):

      'Declare Sleep API
      Private Declare Sub Sleep Lib "kernel32" (ByVal nMilliseconds As Long)

      Hope this helps get you started.

      • #657291

        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

        • #657302

          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

          • #657306

            I would have done it this way

            Private Sub Image1_Click()
              Application.ActiveDocument.FollowHyperlink Address:="http://www.Microsoft.com", _
                NewWindow:=True, AddHistory:=False
            End Sub

            This should allow the default browser (or local application if not going to a html file) to get the gig.

          • #657349

            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

            • #657489

              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

        • #657314

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

    Viewing 0 reply threads
    Reply To: Form with a hyperlink (VBA/Office XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: