Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
828 views
in Technique[技术] by (71.8m points)

vba - Forward Email with its attachment in Outlook 2010

The below code (I pulled from several sources) now works in that when I receive an email with specific words in the subject line it triggers a script that runs the below.

This code then keeps the subject line, adds text the message body and the forwards to the intended recipient.

However, if the email I receive has an attachment the code no longer forwards anything. I need it to forward the attachment that was emailed to me as well (only using the code to add text to body of email otherwise I would just set a rule).

CODE BELOW:

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no need to use the Explorer object in the code:

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

You may find the Getting Started with VBA in Outlook 2010 article helpful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...