用于删除电子邮件的宏

时间:2011-09-26 08:41:33

标签: vba outlook outlook-vba

我创建了一个宏,只要单击一个按钮,就会将电子邮件转发给收件人。但是,我希望宏也删除电子邮件(将其发送到回收站)。

这是当前的代码。这目前可以工作并转发电子邮件。

Sub forwardEmail()

Dim oExplorer As Outlook.Explorer
Dim oMail As Outlook.MailItem
Dim oOldMail As Outlook.MailItem

Set oExplorer = Application.ActiveExplorer
If oExplorer.Selection.Item(1).Class = olMail Then
Set oOldMail = oExplorer.Selection.Item(1)
Set oMail = oOldMail.forward
oMail.Recipients.Add "Recipients email goes here"
oMail.Recipients.Item(1).Resolve
If oMail.Recipients.Item(1).Resolved Then
oMail.Send
Else
MsgBox "Could not resolve " & oMail.Recipients.Item(1).Name
End If
Else
MsgBox "Not a mail item"
End If

End Sub

我想通过在代码中添加oMailItem.Delete可以正常工作,但事实并非如此。

1 个答案:

答案 0 :(得分:2)

我不清楚您要删除哪封电子邮件,原始电子邮件或来自Sent项目的转发电子邮件 - 因此这些mod提供了两种选项。

Sub forwardEmail()
    Dim oExplorer As Outlook.Explorer
    Dim oMail As Outlook.MailItem
    Dim oOldMail As Outlook.MailItem
    Set oExplorer = Application.ActiveExplorer
    If oExplorer.Selection.Item(1).Class = olMail Then
        Set oOldMail = oExplorer.Selection.Item(1)
        Set oMail = oOldMail.Forward
        oMail.Recipients.Add "spam_me"
        oMail.Recipients.Item(1).Resolve
        If oMail.Recipients.Item(1).Resolved Then
            'delete forwarded email from sent items
            oMail.DeleteAfterSubmit = True
            oMail.Send
              'delete original email from inbox
            oOldMail.Delete
        Else
            MsgBox "Could not resolve " & oMail.Recipients.Item(1).Name
        End If
    Else
        MsgBox "Not a mail item"
        End If
End Sub