我需要帮助在 Outlook 2010 中创建 VBA 宏,以便将一个或多个选定邮件转换为纯文本格式。我正在寻找宏而不是传入邮件规则。
我发现以下代码可以作为规则运行,但为了使用它,我必须将消息放入文件夹中并手动运行该规则:
http://www.outlookcode.com/article.aspx?id=62
Sub ConvertToPlain(MyMail As MailItem)
Dim strID As String
Dim objMail As Outlook.MailItem
strID = MyMail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
objMail.BodyFormat = olFormatPlain
objMail.Save
Set objMail = Nothing
End Sub
不久前,我发现了一些代码,它可以从选定的消息中删除所有附件,这完全满足我的需求,如果可能的话,我会尝试复制它的功能;但是,我希望它将消息转换为纯文本,而不是弄乱附件。
http://www.slipstick.com/developer/code-samples/delete-attachments-messages/
Sub RemoveAttachments()
Dim myAttachment As Attachment
Dim myAttachments As Attachments
Dim selItems As Selection
Dim myItem As Object
Dim lngAttachmentCount As Long
' Set reference to the Selection.
Set selItems = ActiveExplorer.Selection
' Loop though each item in the selection.
For Each myItem In selItems
Set myAttachments = myItem.Attachments
lngAttachmentCount = myAttachments.Count
' Loop through attachments until attachment count = 0.
While lngAttachmentCount > 0
myAttachments(1).Delete
lngAttachmentCount = myAttachments.Count
Wend
myItem.Save
Next
MsgBox "All Done. Attachments were removed.", vbOKOnly, "Message"
Set myAttachment = Nothing
Set myAttachments = Nothing
Set selItems = Nothing
Set myItem = Nothing
End Sub
我对将两者结合起来的最佳努力如下:
Sub ConvertPlainText()
Dim selItems As Selection
Dim myItem As Object
Dim lngAttachmentCount As Long
Dim strID As String
Dim objMail As Outlook.MailItem
' Set reference to the Selection.
Set selItems = ActiveExplorer.Selection
' Loop though each item in the selection.
For Each myItem In selItems
Set myAttachments = myItem.Attachments
lngAttachmentCount = myAttachments.Count
strID = MyMail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
objMail.BodyFormat = olFormatPlain
objMail.Save
myItem.Save
Next
MsgBox "All Done. Email converted to Plaintext.", vbOKOnly, "Message"
Set objMail = Nothing
Set selItems = Nothing
Set myItem = Nothing
End Sub
但是我收到一个错误,指出“需要对象”,以以下行开头:
strID = MyMail.EntryID
任何帮助都将不胜感激!!
答案1
在您尝试模仿 RemoveAttachments 时,有很多额外的代码。
Sub ConvertPlainText()
Dim selItems As Selection
Dim myItem As Object
' Set reference to the Selection.
Set selItems = ActiveExplorer.Selection
' Loop through each item in the selection.
For Each myItem In selItems
myItem.BodyFormat = olFormatPlain
myItem.Save
Next
MsgBox "All Done. Email converted to Plaintext.", vbOKOnly, "Message"
Set selItems = Nothing
End Sub
这不是问题,但您可能会发现此代码中也不需要 EntryID。
Sub ConvertToPlain(MyMail As mailItem)
MyMail.BodyFormat = olFormatPlain
MyMail.Save
End Sub