views:

374

answers:

3

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?

A: 

It turns out that ShellExecute isn't the function but Shell, like this:

Sub LaunchURL(Item As Outlook.MailItem)
    Shell ("C:\Program Files\Internet Explorer\IEXPLORE.EXE" & " " & Item.Body)
End Sub
Jake Pearson
+1  A: 

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.

Will Rickards
+1  A: 

You can also use Followhyperlink from VBA to open URLs in the default browser. It can also be used to open documents with the registered application, to send emails and to browse folders.

Remou