问题
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?
回答1:
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.
回答2:
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.
回答3:
Alternatively, use Shell
, like this:
Sub LaunchURL(Item As Outlook.MailItem)
Shell ("C:\Program Files\Internet Explorer\IEXPLORE.EXE" & " " & Item.Body)
End Sub
回答4:
You can create batch file where you write this:
start http://someurl.com/?a=1^&b=2
And you configure Outlook rule to launch this batch file. Notice ^ sign before &. This is escape sequence for & in batch files. Also notice that you need to have default browser set in your Windows OS, almost 100% probability that you have it.
回答5:
Shell ("CMD /C start http://www.spamcop.net"), vbNormalFocus
来源:https://stackoverflow.com/questions/619079/how-to-launch-a-url-when-an-email-arrives