开发者

Macro to delete an email

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.Activ开发者_StackOverfloweExplorer
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.Delete to the code would work but it does not.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜