How to launch a URL when an email arrives

Jake Pearson picture Jake Pearson · Mar 6, 2009 · Viewed 7.2k times · Source

I would like to launch a URL when an email arrives in Outlook. I setup a rule and have it trigger a script function. It looks like I want to call ShellExecute to launch the URL in a browser, but when I hit this line:

    ShellExecute(0&, "open", URL, vbNullString, vbNullString, _
vbNormalFocus)

The method is not defined. Any ideas?

Answer

Will Rickards picture Will Rickards · Mar 6, 2009

ShellExecute is a function in a windows dll. You need to add a declaration for it like this in a VBA module:

Public 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 difference between your Shell solution and ShellExecute is that ShellExecute will use the default system handler for URLs to open the link. This doesn't have to be IE. Your solution will always open it in IE. Yours is the equivalent of putting iexplore.exe into the run box in windows. ShellExecute is the equivalent of just putting the url in the run box in windows.