PDA

View Full Version : Macro to forward email with text and image



dheerajp
08-24-2011, 05:26 AM
[URGENT HELP NEEDED] Trouble with Outlook Macro to forward a mail with text and image
Hi All,

The task that I am trying to accomplish is a bit confusing to explain. So, I'll first attach the code which I am using.


Sub ForwardEmail()
Dim oExplorer As Outlook.Explorer
Dim oMail As Outlook.MailItem
Dim oOldMail As Outlook.MailItem
Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment
Dim olFormat As OlBodyFormat
Dim contentid As String

Set oExplorer = Application.ActiveExplorer
If oExplorer.Selection.Item(1).Class = olMail Then
Set oOldMail = oExplorer.Selection.Item(1)
Set oMail = oOldMail.Forward
oMail.Recipients.Add "xxx@xxx.com"
oMail.Recipients.Item(1).Resolve
If oMail.Recipients.Item(1).Resolved Then

oMail.Subject = "FW: Personalized Subject Line"
oMail.HTMLBody = "Custom Text.<p> <img src=""custom image link"" title=""D"" alt=""D"" name=""D"" border=""0"" id=""D""/>"
'omail.body = "Custom Text" & omail.body
oMail.Save
oMail.Display
Else
MsgBox "Could not resolve " & oMail.Recipients.Item(1).Name
End If
Else
MsgBox "Not a mail item"
End If
End Sub

I have also created a button and a custom keyboard shortcut assigned to this macro.

The macro runs fine, when not using the omail.body code. I get the text, and the image on the forwarded email. But, I do not get the text that was there in the original email.

Now, if I use omail.body code, I get the original text in the email, but the image turns into a hyperlink text.

Hope I am clear enough to explain this issue.

This is all an issue of using .htmlbody and .body in the same macro. And, I need the text from the original mail, and the image as well.

My original email has the image and the text, but i used a <img> tab because, it was not givving me the image when i use macro.

Guys, please help me out. Its very urgent.

Thanks,
Dheeraj P.

dougbert
09-05-2011, 10:14 PM
Hi Dheeraj,

If my solution works for you too, please mark this thread as 'Solved' by using the 'Thread Tools' menu to the right just above your post. Good ratings are also appreciated. :clap:

Try my code. It worked fine for me using <Alt-F8> to launch the macro. Of course, I placed the macro in the ThisOutlookSession module. I tested this on several messages that had text and one or more inline image(s), as well as some that had an attachment too. If the original has LOTS of linked inline images, be patient, as it can take awhile to retrieve all of the original linked images from their server. However, embedded images should load more quickly.

Once you're comfortable with the macro, you can get rid of the .Display line and/or un-comment the .Save and .Send lines if you desire. If you assign it to your button and/or custom keyboard shortcut, the forward will be very automated.


Sub ForwardEmail()

Dim oExplorer As Outlook.Explorer
Dim oMail As MailItem
Set oExplorer = Application.ActiveExplorer
Set oMail = oExplorer.Selection.Item(1).Forward

On Error GoTo Release

If oExplorer.Selection.Item(1).Class = olMail Then
oMail.Subject = "FW: Personalized Subject Line"
oMail.HTMLBody = "Custom Text.<p> <img src=""custom image link""" _
& " title=""D"" alt=""D"" name=""D"" border=""0"" id=""D""/>" _
& vbCrLf & oMail.HTMLBody

oMail.Recipients.Add "xxx@xxx.com"
oMail.Recipients.Item(1).Resolve
If oMail.Recipients.Item(1).Resolved Then
oMail.Display
' oMail.Save
' oMail.Send
Else
MsgBox "Could not resolve " & oMail.Recipients.Item(1).address
End If
Else
MsgBox ("Not a mail item")
End If
Release:
Set oMail = Nothing
Set oExplorer = Nothing
End Sub

dheerajp
09-06-2011, 02:35 AM
Hi doug,

Many thanks to you.. The trick worked. I don't know how I should appreciate your help....
:beerchug:

Regards,
Dheeraj P



Hi Dheeraj,

If my solution works for you too, please mark this thread as 'Solved' by using the 'Thread Tools' menu to the right just above your post. Good ratings are also appreciated. :clap:

Try my code. It worked fine for me using <Alt-F8> to launch the macro. Of course, I placed the macro in the ThisOutlookSession module. I tested this on several messages that had text and one or more inline image(s), as well as some that had an attachment too. If the original has LOTS of linked inline images, be patient, as it can take awhile to retrieve all of the original linked images from their server. However, embedded images should load more quickly.

Once you're comfortable with the macro, you can get rid of the .Display line and/or un-comment the .Save and .Send lines if you desire. If you assign it to your button and/or custom keyboard shortcut, the forward will be very automated.


Sub ForwardEmail()

Dim oExplorer As Outlook.Explorer
Dim oMail As MailItem
Set oExplorer = Application.ActiveExplorer
Set oMail = oExplorer.Selection.Item(1).Forward

On Error GoTo Release

If oExplorer.Selection.Item(1).Class = olMail Then
oMail.Subject = "FW: Personalized Subject Line"
oMail.HTMLBody = "Custom Text.<p> <img src=""custom image link""" _
& " title=""D"" alt=""D"" name=""D"" border=""0"" id=""D""/>" _
& vbCrLf & oMail.HTMLBody

oMail.Recipients.Add "xxx@xxx.com"
oMail.Recipients.Item(1).Resolve
If oMail.Recipients.Item(1).Resolved Then
oMail.Display
' oMail.Save
' oMail.Send
Else
MsgBox "Could not resolve " & oMail.Recipients.Item(1).address
End If
Else
MsgBox ("Not a mail item")
End If
Release:
Set oMail = Nothing
Set oExplorer = Nothing
End Sub

dheerajp
09-13-2011, 03:50 AM
Hi doug,

Can you help me with a small modification.
The code you gave helps me a lot.
But, once i execute it, the original email remains in "Unread" mode. Is there a code which we can add in the macro below, which will make the selected or original email as "Read"?

Regards,
Dheer

JP2112
09-14-2011, 11:37 AM
oExplorer.Selection.Item(1).UnRead = False

JP2112
09-14-2011, 11:39 AM
dougbert,

Your code assumes that the selected item is a mailitem (by assigning it into a MailItem object) before you actually check if the item's Class is olMail.





Sub ForwardEmail()

Dim oExplorer As Outlook.Explorer
Dim oMail As MailItem
Set oExplorer = Application.ActiveExplorer
Set oMail = oExplorer.Selection.Item(1).Forward

On Error GoTo Release

If oExplorer.Selection.Item(1).Class = olMail Then
oMail.Subject = "FW: Personalized Subject Line"
oMail.HTMLBody = "Custom Text.<p> <img src=""custom image link""" _
& " title=""D"" alt=""D"" name=""D"" border=""0"" id=""D""/>" _
& vbCrLf & oMail.HTMLBody

oMail.Recipients.Add "xxx@xxx.com"
oMail.Recipients.Item(1).Resolve
If oMail.Recipients.Item(1).Resolved Then
oMail.Display
' oMail.Save
' oMail.Send
Else
MsgBox "Could not resolve " & oMail.Recipients.Item(1).address
End If
Else
MsgBox ("Not a mail item")
End If
Release:
Set oMail = Nothing
Set oExplorer = Nothing
End Sub

dougbert
09-25-2011, 11:30 PM
dougbert,

Your code assumes that the selected item is a mailitem (by assigning it into a MailItem object) before you actually check if the item's Class is olMail.

Good catch JP! Fortunately, it looks like Dheeraj was focusing on just mail items. So, at least it worked for him as is.

However, to be more correct. Here is the code again with the correct code order...hopefully...just in case someone else might try to use the code in a more general context. I also included the additional line you noted.


Sub ForwardEmail()
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 = oExplorer.Selection.Item(1).Forward
oMail.Subject = "FW: Personalized Subject Line"
oMail.HTMLBody = "Custom Text.<p> <img src=""custom image link""" _
& " title=""D"" alt=""D"" name=""D"" border=""0"" id=""D""\>" _
& vbCrLf & oMail.HTMLBody
oMail.Recipients.Add "xxx.xxx.com"
oMail.Recipients.Item(1).Resolve
If oMail.Recipients.Item(1).Resolved Then
oExplorer.Selection.Item(1).UnRead = False
oMail.Display
' oMail.Save
' oMail.Send
Else
MsgBox "Could not resolve " & oMail.Recipients.Item(1).Address
End If
Else
MsgBox ("Not a mail item")
End If
Release:
Set oMail = Nothing
Set oExplorer = Nothing
End Sub


Did I get it right that time, JP? I hope so!

fhimage
06-20-2013, 09:10 PM
the solution work for me , i hope it will help you :yes