Forward email with its attachment and text from template

97 Views Asked by At

I need to forward a selected email (which I call origEmail) with its attachment and additional text.

The code leaves off the attachment of origEmail.

The body of the forward email is taken from a template email and is merged with origEmail body.

How do I include the attachment of origEmail?

Sub forwardemailwithattachment()

    Dim origEmail As MailItem
    Dim forwardEmail As MailItem

    Set origEmail = Application.ActiveWindow.Selection.Item(1)
    Set forwardEmail = Application.CreateItemFromTemplate("insert here template location")

    forwardEmail.To = "insert here email address of recipient"

    forwardEmail.Subject = forwardEmail.Subject & origEmail.Subject

    forwardEmail.HTMLBody = forwardEmail.HTMLBody & origEmail.Forward.HTMLBody

    forwardEmail.Display

End Sub
3

There are 3 best solutions below

1
Black cat On

You can try this:

pathto = "C:\YourPath\"
For i = 1 to origEmail.Attachments.Count
  origEmail.Attachments(i).SaveAsFile pathto & origEmail.Attachments(i).FileName
  forwardEmail.Attachments.Add pathto & origEmail.Attachments(i).FileName
  Kill pathto & origEmail.Attachments(i)
Next i
8
Dmitry Streblechenko On

Call MailItem.Forward (returns the new forwarded MailItem object with the suitably formatted body and the attachments), then modify its HTMLBody with your extra data. Keep in mind that two HTML strings cannot be simply concatenated - they must be merged.

3
Eugene Astafiev On

To create a new item in case of forwarding you need to use a corresponding method for that, see MailItem.Forward.

Your code may look like that:

Sub forwardemailwithattachment()

    Dim origEmail As MailItem
    Dim forwardEmail As MailItem

    Set origEmail = Application.ActiveWindow.Selection.Item(1)
    Set forwardEmail = origEmail.Forward()

    forwardEmail.To = "insert here email address of recipient"

    forwardEmail.Display

End Sub

Note, in case of calling the Forward method there is no need to set the message body or set the Subject property separately. They will be set by default based on the original item properties.