vba Outlook 规则 将电子邮件保存为文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27569093/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:35:29  来源:igfitidea点击:

Outlook Rule Save email to text

vbaemailoutlook

提问by Auron5500

I'm having trouble with automatically exporting the body of an email into a text file using a script. I've managed a script that will save the text into a file on a macro but that won't work on a rule which is what I need.

我在使用脚本将电子邮件正文自动导出到文本文件时遇到了问题。我管理了一个脚本,该脚本将文本保存到宏文件中,但这不适用于我需要的规则。

My current code is as follows:

我目前的代码如下:

Sub SaveAsTXT()
 Dim myItem As Outlook.Inspector
 Dim objItem As Object
 Dim myFolder As Folder



 Set myItem = Application.ActiveInspector
 If Not TypeName(myItem) = "Nothing" Then

        Set myNamespace = Application.GetNamespace("MAPI")
        Set myFolder = myNamespace.GetDefaultFolder(olFolderInbox)

 Set objItem = myItem.CurrentItem
 strname = objItem.Subject
 strdate = Format(objItem.ReceivedTime, " yyyy mm dd")
 objItem.SaveAs "c:\users\philip\documents\" & strname & strdate & ".txt", olTXT
    End If

End Sub

Apologies if it looks a bit messy, I've edited it countless times trying to get it to work.

抱歉,如果它看起来有点乱,我已经无数次编辑它试图让它工作。

That's the code that will correctly run when I'm in the open email and run it as a macro but it won't work correctly when run as a rule I have tried amending to Sub SaveAsTXT(Item as Outlook.Mailitem)but this also doesn't seem to work

这是当我打开电子邮件并将其作为宏运行时将正确运行的代码,但是当我尝试修改的规则运行时它将无法正常运行,Sub SaveAsTXT(Item as Outlook.Mailitem)但这似乎也不起作用

So basically the question is how to I ensure the code will select the email (which will always be entitled "Rotas" without quotes) when it is run as a rule?

所以基本上问题是如何确保代码在正常运行时选择电子邮件(它总是被称为“Rotas”而不带引号)?

Info: Using office 2010 and I'm not a very good coder to start with.

信息:使用 office 2010,我不是一个很好的编码员。

回答by Auron5500

Actually I managed to sort it out myself.

其实是我自己解决的。

I didn't consider that the item as Outlook.Mailitemelement was actually the thing that was selected by the rule. So I applied itemas the object rather than objItem

我没有考虑到item as Outlook.Mailitem元素实际上是规则选择的东西。所以我申请item作为对象而不是objItem

Find the successful (and cleaned up) code below:

在下面找到成功(并清理)的代码:

Sub SaveAsTXT(myMail As Outlook.MailItem)

 Dim objItem As Object
 Dim myFolder As Folder


 If Not TypeName(myitem) = "Nothing" Then

        If myMail.Subject = "Rotas" Then

 strname = myMail.Subject
 strdate = Format(myMail.ReceivedTime, " yyyy mm dd")
 myMail.SaveAs "c:\users\philip\documents\" & strname & ".txt", olTXT
    End If


 End If

End Sub