Thursday, July 31, 2008

How to launch a URL from VBA in Microsoft Dynamics GP

So you have created this great customization and part of it is to launch a URL and pass in some parameters via http. For example, a customization on the customer master that will open the customer's invoice entry portal where you can type in the invoice information.

The following code can be placed in the (General) section of your VBA customization and be called from any other event, for example the BeforeUserChanged event.

Public Function OpenBrowser(ByVal URL As String) As Boolean
Dim res As Long

' it is mandatory that the URL is prefixed with http:// or https://

If InStr(1, URL, "http", vbTextCompare) <> 1 Then
URL = "http://" & URL
End If

result = ShellExecute(0, "open", URL, vbNullString, vbNullString, vbNormalFocus) OpenBrowser = (result > 32)

End Function

You can call the ShellExecute() Windows API function from a Visual Basic for Applications in Microsoft Dynamics GP to start another program under Microsoft Windows. Use ShellExecute() instead of Shell (a Visual Basic statement) or WinExec() (a Windows API function) to work around the following limitation of the latter commands: With Shell and WinExec(), you cannot start an application by specifying a file name only. ShellExecute() uses the default programs associated to each file type. In the case of the above example, ShellExecute() automatically recognizes the prefix http in the URL and launches Internet Explorer (or the default browser application).

Other Resources

KB Article 224816 - Use ShellExecute to Launch the default browser, by Microsoft Support.

Until next post!

Mariano Gomez, MIS, MVP, MCP, PMP
Maximum Global Business, LLC


Anonymous said...

To make this work you just need to add the following to your VBA so that the ShellExecute does something... cheers Robert

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

Mariano Gomez said...


Much thanks for the input! It's always recommended to prototype Win32 functions by referencing the exact library file (DLL), which in turn will force VB/VBA to load the DLL in the context of the running application, rather than just assume the libraries are loaded automatically when COM is initialized.

Good catch!

Mariano Gomez, MVP, MCP
Maximum Global Business, LLC

Anonymous said...

Hi there,

I have tested your code to launch a Crystal 11 report which works well but I would like to pass a parameter/s and refresh the report with the parameter/s passed. Could you help me please?