Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
830 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...