vba 删除电子邮件的宏

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

Macro to delete an email

vbaoutlookoutlook-vba

提问by YokoBlac

I have created a macro that forwards an email on to a recipient once a button is clicked. However, I want the macro to also delete the email (sending it to the recycle bin).

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

Here is the current code. This currently works and forwards the email.

这是当前的代码。这目前有效并转发电子邮件。

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

I thought by adding oMailItem.Deleteto the code would work but it does not.

我认为通过添加oMailItem.Delete到代码会起作用,但它不会。

采纳答案by brettdj

It wasn't clear to me which email you wanted deleted, the original email or the forwarded email from Sentitems - so these mods provide both options.

我不清楚您要删除哪封电子邮件,原始电子邮件还是来自Sent项目的转发电子邮件- 所以这些 mods 提供了这两种选择。

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