转发电子邮件及其在Outlook 2010中的附件

时间:2015-03-03 19:15:20

标签: vba outlook-vba email-attachments

以下代码(我从多个来源提取)现在的工作原理是,当我收到主题行中包含特定单词的电子邮件时,它会触发运行以下内容的脚本。

然后,此代码保留主题行,将邮件正文和转发文本添加到预期收件人。

但是,如果我收到的电子邮件附件,则代码不再转发任何内容。我需要它来转发通过电子邮件发送给我的附件(仅使用代码将文本添加到电子邮件正文中,否则我只会设置规则)。

以下代码:

Sub ForwardEmail(item As Outlook.MailItem)
Dim oExplorer As Outlook.Explorer
Dim oMail As MailItem
Set oExplorer = Application.ActiveExplorer

On Error GoTo Release

If oExplorer.Selection.item(1).Class = olMail Then
Set oMail = item.Forward
oMail.Subject = oMail.Subject
oMail.HTMLBody = "Have a nice day." & vbCrLf & oMail.HTMLBody
oMail.Recipients.Add "email address here"


oMail.Save
oMail.Send

End If
Release:
Set oMail = Nothing
Set oExplorer = Nothing
End Sub

2 个答案:

答案 0 :(得分:6)

无需在代码中使用Explorer对象:

Sub ForwardEmail(item As Outlook.MailItem)
  Dim oMail As MailItem    

  On Error GoTo Release

  If item.Class = olMail Then
     Set oMail = item.Forward
     oMail.Subject = oMail.Subject
     oMail.HTMLBody = "Have a nice day." & vbCrLf & oMail.HTMLBody
     oMail.Recipients.Add "email address here"

     oMail.Save
     oMail.Send
  End If
 Release:
  Set oMail = Nothing
  Set oExplorer = Nothing
End Sub

您可能会发现Getting Started with VBA in Outlook 2010文章有用。

答案 1 :(得分:2)

有一个不必要的条件

If oExplorer.Selection.item(1).Class = olMail Then

可能会导致绕过转发。

Sub ForwardEmail(item As Outlook.MailItem)
' Dim oExplorer As Outlook.Explorer
Dim oMail As MailItem
' Set oExplorer = Application.ActiveExplorer

On Error GoTo Release

' If oExplorer.Selection.item(1).Class = olMail Then

Set oMail = item.Forward
oMail.Subject = oMail.Subject
oMail.HTMLBody = "Have a nice day." & vbCrLf & oMail.HTMLBody
oMail.Recipients.Add "email address here"

' oMail.Save
oMail.Send

' End If

Release:
Set oMail = Nothing
' Set oExplorer = Nothing
End Sub