Forward email with your attachment in Outlook 2010

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

In this code, the subject line is saved, the text of the message body and the forward is added to the intended recipient.

However, if the email I receive has an attachment, the code no longer sends anything. I need to forward the attachment that was sent to me by email (only using the code to add text to the body of the 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 
+6
source share
2 answers

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 can find Getting Started with VBA in Outlook 2010 .

+5
source

There is an unnecessary condition

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

which can lead to bypass forwarding.

 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
source

Source: https://habr.com/ru/post/983275/


All Articles