VBA Snippets: Execute URL or Application

MicrosoftThis post is part of the series on VBA Snippets.

it is possible to execute a URL or application in VBA using the Windows Shell Execute API function. In this snippet I am executing a URL, but this could be an application.

Before you can call ShellExecute in code you need to add the following line to the declarations at the top of the mdoule:

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

The following VBA command will execute the supplied URL (or application); the highlighted variable contains the URL:

iResult = ShellExecute(0, "open", sReportURL, vbNullString, vbNullString, vbNormalFocus)

You can then use the iResult for error handling.