vba 将 Outlook 电子邮件另存为文本文件,然后在其上运行脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17703545/
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
Save Outlook Email as text file, and then run a script on it?
提问by David Y. Stephenson
I'm using Outlook 2007. I have no experience with Windows programming or VBA, as all of my background is on Unix systems. I am attempting to have Outlook save each incoming emails as text file, and then a run a python script which will parse this text file.
我使用的是 Outlook 2007。我没有 Windows 编程或 VBA 的经验,因为我的所有背景都在 Unix 系统上。我试图让 Outlook 将每个传入的电子邮件保存为文本文件,然后运行一个 python 脚本来解析这个文本文件。
I found thisquestion which seems to provide some VBA code to accomplish the first task, but I'm not sure how to utilize it. I open the Visual Basic Editor, but have no idea how to proceed from there. I tried saving the text as a module, but when I attempt add "run a script" in an Outlook rule, no scripts are available. What am I doing wrong?
我发现这个问题似乎提供了一些 VBA 代码来完成第一个任务,但我不确定如何利用它。我打开了 Visual Basic 编辑器,但不知道如何从那里开始。我尝试将文本保存为模块,但是当我尝试在 Outlook 规则中添加“运行脚本”时,没有可用的脚本。我究竟做错了什么?
EDIT: Clarification.
编辑:澄清。
回答by Geoff
I'm assuming you're familiar with coding at somelevel, even if not on Windows. You may want to do some general background reading on Outlook VBA; some resources on this would be a Microsoft article, this articlefrom OutlookCode, and so on - there's a ton of resources out there with walkthroughs and examples.
我假设您在某种程度上熟悉编码,即使不是在 Windows 上。你可能想阅读一些关于 Outlook VBA 的一般背景资料;这方面的一些资源可能是Microsoft 文章、OutlookCode 中的这篇文章,等等 - 有大量资源,包括演练和示例。
Addressing your specific question: take a look at this Microsoft KB article, which describes how to trigger a script from a rule.
解决您的具体问题:查看这篇 Microsoft 知识库文章,它描述了如何从规则触发脚本。
The key to getting started once you've gotten your VBA editor open is to double-click a module on the left, for example ThisOutlookSession
(under 'Microsoft Outlook Objects').
That should give you an editor you can paste code into. Bear in mind that (per the above MS page) your procedure must accept a MailItem object, which will be the item that the rule has in hand, so the linked example you gave would have the first couple of lines changed from:
例如,打开 VBA 编辑器后开始使用的关键是双击左侧的模块ThisOutlookSession
(在“Microsoft Outlook 对象”下)。这应该会给你一个编辑器,你可以将代码粘贴到其中。请记住,(根据上面的 MS 页面)您的过程必须接受一个 MailItem 对象,这将是规则手头的项目,因此您提供的链接示例的前几行将从以下内容更改:
Sub SaveEmail()
Dim msg As Outlook.MailItem
' assume an email is selected
Set msg = ActiveExplorer.Selection.Item(1)
' save as text
[...]
...to:
...到:
Sub SaveEmail(msg As Outlook.MailItem)
' save as text
[...]
Essentially you're being handed a MailItem rather than having to create it and connect it to the selected item in Outlook.
本质上,您将收到一个 MailItem,而不必创建它并将其连接到 Outlook 中的选定项目。
To achieve the second task of 'running a script' on the file, I'm assuming that you want your VBA to make changes to the file after it's been saved? This is pretty straightforward in VBA, and you'll find lots of examples for it. One pretty simple outline is in this answer.
为了实现对文件“运行脚本”的第二个任务,我假设您希望 VBA 在文件保存后对其进行更改?这在 VBA 中非常简单,你会找到很多例子。这个答案中有一个非常简单的大纲。
Edit based on comments:to launch an external tool, you can use either the Shell
command if you don't need to wait for it to complete, or you can use one of the many Shell-and-wait implementations floating around, for example this popular one. Or, you can use the WScript
approach in this answer.
根据评论进行编辑:要启动外部工具,Shell
如果您不需要等待它完成,您可以使用该命令,或者您可以使用浮动的许多 Shell-and-wait 实现之一,例如这个流行一。或者,您可以使用此答案中的WScript
方法。
Note that you'll need to ensure Outlook is set to allow macros to run, and you will probably want to sign your code.
请注意,您需要确保 Outlook 设置为允许运行宏,并且您可能希望对您的代码进行签名。