Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Using image tags to embed an image into an Outlook email

Hi

I was given the code below to insert html into the body of a message.
That works well but I want to insert an image in a folder instead of just text
using image tags. Something like  "<img .../><img .../><img .../>"
What code would I use to specify two images from different  folders?
Sub CreateHTMLMail()
'Creates a new e-mail item and modifies its properties

    Dim olApp As Outlook.Application
    Dim objMail As Outlook.MailItem
    Set olApp = Outlook.Application
    'Create e-mail item
    Set objMail = olApp.CreateItem(olMailItem)

    With objMail
       'Set body format to HTML
       .BodyFormat = olFormatHTML
       .HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2><BODY>Please enter the message text here. </BODY></HTML>"
      

       .Display
    End With

Open in new window

Avatar of Mechanic_Kharkov
Mechanic_Kharkov
Flag of Ukraine image

Images in HTML part of mail message can be inserted as external links. You can not send folders via email. =(
You can attach files to email message, but no folders.
And how to embed external link to HTML is not a miracle. (e.g. look source of EE notification email message.html - it contents images).
Avatar of Murray Brown

ASKER

Hi. I don't want to email folders. As I said above I want to send an image as the html body of the message. I want the code to take this image from a specific folder.
Not sure, but it seems that you should gig to Attachments property of mail Item.
Something like that:

    objMail.Save
    Set myAttachments = objMail.Attachments
    myAttachments.Add "C:\My Documents\Q496.jpg", _
      MyAttachType, MyPosition, "Text replacement of attachment"
   
where MyAttachType is [optional] type of attach (maybe "image/jpeg"), and MyPosition is the position of attach in message body. (0 = hidden, 1 - Before the text, and so on).

Details in MSDN
http://msdn.microsoft.com/en-us/library/aa220070%28v=office.11%29.aspx
and
http://msdn.microsoft.com/en-us/library/aa210901%28v=office.11%29.aspx

Not sure how to calc position in the body when it's in HTML format. But MS tells that BodyFormat can be changed from RTF to HTML and vice versa in any time.
Maybe it's possible to obtain each attachment Id to use it in IMG SRC tag of HTML using Attachments(n) construction.
I read it that you want to have something similar to a tag in a web page, where you just click the tag to obtain the full-size image. I cannot see that it is possible to hide a full-size image in an html message - but you could do it the same way: You create a tag that you insert into the message as a small-size picture. The tag you link to the full/size image in a public location just as you would do for the web-page.
If the purpose is to distribute images you might automate this with a script that asks you to browse for the image and inserts it automatically.
If you just want to have a single to a couple of  these images you might create "Outlook templates" from email messages (Save as "Outlook template (.oft)) for the different messages, or a Draft, where you create all your Tags with attached links. When you copy the tag to your message the link goes with it.
The tag you create from a copy of  the full-size image that you shrink in e. g. Microsoft Office Picture Manager to a convenient size. You insert the tag into your email message and insert the URL address to the public location into a hyperlink attached to the tag.
To make a good script you might make a VB macro including browsing for the image in the file system and programmatic generation of html code to be inserted into the message just like Mechanic_Charkov (¿¿¿¿¿¿¿_¿¿¿¿¿¿) advices.
 
Please could someone give the code needed  in the code I supplied above to make this happen
Avatar of David Lee
Hi, murbro.

This should do it. You'll need to edit the URLs of the images and position them as you need them.
Sub CreateHTMLMail()
'Creates a new e-mail item and modifies its properties

    Dim olApp As Outlook.Application
    Dim objMail As Outlook.MailItem
    Set olApp = Outlook.Application
    'Create e-mail item
    Set objMail = olApp.CreateItem(olMailItem)

    With objMail
       'Set body format to HTML
       .BodyFormat = olFormatHTML
       .HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2>" _
                 & "<BODY>Please enter the message text here." _
                 & "<img src=""http://www.company.com/image1"" />" _
                 & "<img src=""http://www.company.com/image2"" />" _
                 & "</BODY></HTML>"
      

       .Display
    End With

Open in new window

Hi. Thanks very much. How do I specify the image source as a folder on my computer.
The images are in the folder
MYPIC_DIR = Environ("userprofile") & "\my documents\Pictures"
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks very much once again. Appreciate the help
You''re welcome.  Always happy to be of service.