vba 如何在电子邮件到达时启动 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/619079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to launch a URL when an email arrives
提问by Jake Pearson
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:
我想在电子邮件到达 Outlook 时启动一个 URL。我设置了一个规则并让它触发一个脚本函数。看起来我想调用 ShellExecute 在浏览器中启动 URL,但是当我点击这一行时:
ShellExecute(0&, "open", URL, vbNullString, vbNullString, _
vbNormalFocus)
The method is not defined. Any ideas?
该方法未定义。有任何想法吗?
回答by Will Rickards
ShellExecute is a function in a windows dll. You need to add a declaration for it like this in a VBA module:
ShellExecute 是 Windows dll 中的一个函数。您需要在 VBA 模块中为它添加这样的声明:
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.
您的 Shell 解决方案和 ShellExecute 之间的区别在于 ShellExecute 将使用 URL 的默认系统处理程序来打开链接。这不一定是 IE。您的解决方案将始终在 IE 中打开它。您的相当于将 iexplore.exe 放入 Windows 的运行框中。ShellExecute 相当于将 url 放在 Windows 的运行框中。
回答by Fionnuala
You can also use Followhyperlinkfrom 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.
您还可以使用VBA 中的Followhyperlink在默认浏览器中打开 URL。它还可用于使用已注册的应用程序打开文档、发送电子邮件和浏览文件夹。
回答by meir
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.
并且您配置 Outlook 规则以启动此批处理文件。注意 & 前的 ^ 符号。这是批处理文件中 & 的转义序列。另请注意,您需要在 Windows 操作系统中设置默认浏览器,几乎 100% 的可能性您拥有它。
回答by MrCalvin
Shell ("CMD /C start http://www.spamcop.net"), vbNormalFocus
回答by Jake Pearson
Alternatively, use Shell
, like this:
或者,使用Shell
,如下所示:
Sub LaunchURL(Item As Outlook.MailItem)
Shell ("C:\Program Files\Internet Explorer\IEXPLORE.EXE" & " " & Item.Body)
End Sub