在 Outlook 2010 中转发带有附件的电子邮件
Forward Email with its attachment in Outlook 2010
下面的代码(我从几个来源中提取)现在可以工作,因为当我收到一封主题行中包含特定单词的电子邮件时,它会触发一个脚本来运行下面的代码。
此代码然后保留主题行,在邮件正文中添加文本并转发给预期的收件人。
但是,如果我收到的电子邮件有附件,代码将不再转发任何内容。我也需要它来转发通过电子邮件发送给我的附件(仅使用代码将文本添加到电子邮件正文中,否则我只会设置规则)。
下面的代码:
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
存在不必要的条件
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
代码中不需要使用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 文章有帮助。
下面的代码(我从几个来源中提取)现在可以工作,因为当我收到一封主题行中包含特定单词的电子邮件时,它会触发一个脚本来运行下面的代码。
此代码然后保留主题行,在邮件正文中添加文本并转发给预期的收件人。
但是,如果我收到的电子邮件有附件,代码将不再转发任何内容。我也需要它来转发通过电子邮件发送给我的附件(仅使用代码将文本添加到电子邮件正文中,否则我只会设置规则)。
下面的代码:
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
存在不必要的条件
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
代码中不需要使用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 文章有帮助。