vba 将电子邮件保存为文本文件的宏,用于规则
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21611665/
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
Macro to save e-mail as text file, to be used in a rule
提问by MelBurslan
My problem is very similar to this threadand this one. I think my issue is to combine these two questions.
我的问题与此线程和此线程非常相似。我认为我的问题是将这两个问题结合起来。
I am running:
我在跑步:
- OS: Windows 7 Enterprise Professional
- Outlook 2010
- VBA version 7.0
- 操作系统:Windows 7 Enterprise Professional
- 展望 2010
- VBA 7.0 版
By reading these two questions as well as some other pages from Microsoft and elsewhere, I was able to open the VB editor and paste into it, this simple code:
通过阅读这两个问题以及 Microsoft 和其他地方的一些其他页面,我能够打开 VB 编辑器并将其粘贴到其中,这个简单的代码:
Sub SaveEmail(msg As Outlook.MailItem)
' save as text
msg.SaveAs "C:\Users\mel\mailsave\email.txt" & Format(Now, "YYYYMMDDHHMMSS"), _
olTXT
End Sub
- Is the "format" portion of my msg.SaveAs line, going to save a unique text file for each email matching my rule?
- How do I run this macro to test and if successful, how do I run it repeatedly?
- 我的 msg.SaveAs 行的“格式”部分是否会为每封符合我的规则的电子邮件保存一个唯一的文本文件?
- 如何运行此宏进行测试,如果成功,如何重复运行?
I tried going to the run menu and selecting run "sub/user form" item but the next dialog box is asking what to run and does not populate a list of macros available for running. Clicked on "save" icon but nothing changed.
我尝试转到运行菜单并选择运行“子/用户表单”项,但下一个对话框询问要运行的内容并且未填充可用于运行的宏列表。单击“保存”图标,但没有任何改变。
回答by DanL
Specifying a method with that signature (Sub method (var As Outlook.MailItem)
) allows you to use the method when creating a mailbox rule. As far as I understand your question, you're beyond that point.
指定具有该签名 ( Sub method (var As Outlook.MailItem)
) 的方法允许您在创建邮箱规则时使用该方法。据我了解你的问题,你已经超出了这一点。
Question 1
问题 1
The format portion of your code is only going to save a unique file at most once per second. You're appending the current date and time to the file. Your main problem, however, is not the timestamp, but the file format. You should apply the timestamp before the file extension, e.g.
代码的格式部分最多只能每秒保存一次唯一文件。您将当前日期和时间附加到文件中。但是,您的主要问题不是时间戳,而是文件格式。您应该在文件扩展名之前应用时间戳,例如
msg.SaveAs "C:\Users\mel\mailsave\email" & Format(Now, "YYYYMMDDHHMMSS") & ".txt", olTXT
Question 2
问题2
If you add the macro to a rule, it will be run when the rule is matched. The macro can be tested by creating a method that grabs the currently selected mail, e.g.
如果将宏添加到规则中,它将在规则匹配时运行。可以通过创建一个获取当前选择的邮件的方法来测试宏,例如
Sub TestSaveEmail()
Call SaveEmail(ActiveExplorer.Selection(1))
End Sub
This macro can then be run by setting the cursor within the method and pressing F5.
然后可以通过在方法内设置光标并按 F5 来运行该宏。
The macro can also be added to the Outlook user interface by customizing the ribbon and adding a macro button. For help on customizing the ribbon, refer to the following article:
还可以通过自定义功能区和添加宏按钮将宏添加到 Outlook 用户界面。有关自定义功能区的帮助,请参阅以下文章: