Results 1 to 5 of 5

Thread: VB - Embedd an image into an Outlook email message body

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    VB - Embedd an image into an Outlook email message body

    Instead of adding an image to the body of a message with a web
    link (img scr='http://somewhere.com'). This code will show how to
    embedd the image like when you click on Insert > Picture...

    VB Code:
    1. Option Explicit
    2. 'Add reference to MS Outlook x.x Object Library
    3. 'Picture to be added as an attachment and modified src location for each embedded picture.
    4. Private Sub Command1_Click()
    5.  
    6.     Dim oApp As Outlook.Application
    7.     Dim oEmail As MailItem
    8.     Dim colAttach As Outlook.Attachments
    9.     Dim oAttach As Outlook.Attachment
    10.    
    11.     'create new Outlook MailItem
    12.     Set oApp = CreateObject("Outlook.Application")
    13.     Set oEmail = oApp.CreateItem(olMailItem)
    14.     'add graphic as attachment to Outlook message
    15.     'change path to graphic as needed
    16.     Set colAttach = oEmail.Attachments
    17.     Set oAttach = colAttach.Add("D:\my documents\[color=red]MyPic.jpg[/color]")
    18.     oEmail.Close olSave
    19.     'change the src property to 'cid:your picture filename'
    20.     'it will be changed to the correct cid when its sent.
    21.     oEmail.HTMLBody = "<BODY><FONT face=Arial color=#000080 size=2></FONT>" & _
    22.     "<IMG alt='' hspace=0 src='[color=red]cid:MyPic.jpg[/color]' align=baseline border=0>&nbsp;</BODY>"
    23.     oEmail.Save
    24.     oEmail.Display 'fill in the To, Subject, and Send. Or program it in.
    25.     Set oEmail = Nothing
    26.     Set colAttach = Nothing
    27.     Set oAttach = Nothing
    28.     Set oApp = Nothing
    29.  
    30. End Sub
    When the message is displayed it will look like
    its not embedded correctly, but when Outlook sends the
    message it will embedd it and link the proper source cid.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: VB - Embedd an image into an Outlook email message body

    thanks man. a simple Qs. why do i need to save the oEmail?

    vb Code:
    1. oEmail.Save

    one thing more, when i removed the attached image and send the images not also showing.
    the code work but its kinda odd to attach the image(company logo at complimentary clause) for every mail created?
    Last edited by jlbantang; Aug 31st, 2008 at 12:07 AM.

  3. #3
    New Member
    Join Date
    Jul 2009
    Posts
    3

    Re: VB - Embedd an image into an Outlook email message body

    How to send picture into new message while Outlook is opening ?
    Tell me about do it ?
    Thanks !

  4. #4
    Member
    Join Date
    Jan 2011
    Posts
    48

    Re: VB - Embedd an image into an Outlook email message body

    Hey i tried this code but i am not getting my image embedded in the body of email. Can u tel me why please. Here is the code:
    Dim oApp As Outlook.Application
    Dim oEmail As MailItem
    Dim colAttach As Outlook.Attachments
    Dim oAttach As Outlook.Attachment

    'create new Outlook MailItem
    Set oApp = CreateObject("Outlook.Application")
    Set oEmail = oApp.CreateItem(olMailItem)
    'add graphic as attachment to Outlook message
    'change path to graphic as needed
    Set colAttach = oEmail.Attachments
    'Set oAttach = colAttach.Add("D:\my documents\MyPic.jpg")
    Set oAttach = colAttach.Add("C:\TITAN\imagefile.jpeg")
    'change the src property to 'cid:your picture filename'
    'it will be changed to the correct cid when its sent.


    With oEmail
    .To = "anselseenu2888@gmail.com"
    .Subject = "TITAN DAILY REPORT"
    '.Body = "Please find the TITAN daily report pie chart"& vbNewLine& "C:\TITAN\imagefile.jpeg"
    '.Body = "Please find the pie chart"

    .HTMLBody = "<BODY><FONT face=Arial color=#000080 size=2></FONT>" & _
    "<IMG alt='' hspace=0 src='cid:C:\TITAN\imagefile.jpeg' align=baseline border=0>&nbsp;</BODY>"
    oEmail.Save
    oEmail.Display 'fill in the To, Subject, and Send. Or program it in.


    '.Attachments.Add "\\server\drive\folder\filename", olByValue, 1
    .Send
    End With
    'oEmail.Close olSave
    MsgBox "Email sent"
    Set oEmail = Nothing
    Set colAttach = Nothing
    Set oAttach = Nothing
    Set oApp = Nothing

    End Sub

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB - Embedd an image into an Outlook email message body

    In your related thread in the Classic VB section, see if one of the issues regarding how you are creating the jpg file is the cause.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width