宏以删除电子邮件 [英] Macro to delete an email

查看:152
本文介绍了宏以删除电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我以为在代码中添加oMailItem.Delete可以,但是没有用.

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

推荐答案

我不清楚您要删除哪个电子邮件,原始电子邮件或Sent项目转发的电子邮件-因此这些模块提供了这两个选项.

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

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

这篇关于宏以删除电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆