Share via

VBA Code to open url from a command button in Word 2016

Anonymous
2021-10-25T18:10:19+00:00

Hi

I am trying to open a url from the click of a command button. Ideally this button would continue work after the document has been saved as a pdf.

Can anyone direct me to example code? I am somewhat familiar with vba and macros but it has been a few years since I wrote code.

Microsoft 365 and Office | Word | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. Charles Kenyon 166.6K Reputation points Volunteer Moderator
    2021-10-28T14:19:18+00:00

    Note, AFAIK, a command button, which is a vba construct, will not work in a pdf file. Nor will it work in Word on a Mac. A hyperlink field, though, will. Here is a screenshot of a hyperlink field formatted with a text border and red color.

    That should work on a pdf and would work in a Mac version of Word.

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2021-10-26T03:38:25+00:00

    The following function will open a URL (here my web page) in your default browser when called from the macro GetWebPage. ****

    It will not run in a PDF file.

    Call the GetWebPage macro from your button,

    Option Explicit

    'Graham Mayor – https://www.gmayor.com – Last updated – 26 Oct 2021

    Private pWebAddress As String

    #If VBA7 Then

    Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _

            ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, _

            ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As LongPtr) As LongPtr

    Public Sub NewShell(cmdLine As String, lngWindowHndl As LongPtr)

        ShellExecute lngWindowHndl, "open", cmdLine, "", "", 1

    lbl_Exit:

        Exit Sub

    End Sub

    #Else

    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _

                                          ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _

                                          ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

    Public Sub NewShell(cmdLine As String, lngWindowHndl As Long)

        ShellExecute lngWindowHndl, "open", cmdLine, "", "", 1

    lbl_Exit:

        Exit Sub

    End Sub

    #End If

    Public Sub GetWebPage()

     pWebAddress = "https://www.gmayor.com/"

        Call NewShell(pWebAddress, 3)

    lbl_Exit:

        Exit Sub

    End Sub

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2021-10-28T10:41:24+00:00

    Hi,

    I am following up on this thread to know if you have seen the provided suggestions. Feel free to share updates at your convenience time.

    Regards,

    Christophe

    0 comments No comments
  4. Suzanne S Barnhill 277.1K Reputation points MVP Volunteer Moderator
    2021-10-26T11:25:46+00:00

    If you insert a hyperlink to the URL in your document, users can access it using Ctrl+click. The "Text to display" can be something other than the URL (including an image). This link will still work in a PDF.

    0 comments No comments