Consulting

Results 1 to 12 of 12

Thread: Embed image into body mail

  1. #1
    VBAX Tutor
    Joined
    Sep 2007
    Posts
    265
    Location

    Embed image into body mail

    Hi there.

    Hope is everything running well.

    Please help me on how to embed image into bodymail using excel and outlook.

    Here is what i've done
    sub test()
    Dim OL As Object, rng As Range
    Dim EmailItem As Object
    Const mypath As String = "d:\My Pictures\"
           Application.ScreenUpdating = False
        Set OL = CreateObject("Outlook.Application")
    Set EmailItem = OL.CreateItem(0)
        EmailItem.Attachments.Add mypath & "AYANA-emailblast-100211.jpg"
        EmailItem.HTMLBody = "<html><p>This is a picture.</p>" & _
    "<img src='cid:pictest.jpg' height=480 width=360>"
        EmailItem.Display
    End sub
    I can't see the picture with the above code
    Please find the attached sample picture.

    Thank you in advance.
    Regards,
    Harto
    Last edited by Aussiebear; 04-09-2023 at 04:44 PM. Reason: Adjusted the code tags

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Try this Harto:

    Sub test()
        Dim OL As Object, rng As Range
        Dim EmailItem As Object
        Const mypath As String = "d:\My Pictures\"
        Application.ScreenUpdating = False
        Set OL = CreateObject("Outlook.Application")
    Set EmailItem = OL.CreateItem(0)
    '    EmailItem.Attachments.Add mypath & "AYANA-emailblast-100211.jpg"
        EmailItem.HTMLBody = "<html><p>This is a picture.</p>" & _
        "<img src=C:\Users\Steve\Documents\pictest.jpg' height=480 width=360>"
        EmailItem.Display
    End Sub
    Last edited by Aussiebear; 04-09-2023 at 04:45 PM. Reason: Adjusted the code tags
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Tutor
    Joined
    Sep 2007
    Posts
    265
    Location
    Thanks for the reply, Lucas.

    i'll test the code given later as we've public holiday here.
    Have a nice weekend

  4. #4
    VBAX Tutor
    Joined
    Sep 2007
    Posts
    265
    Location
    Hi Steve,

    Still have the same problem. I can;t see the picture.

    Please advise
    Thanks a bunch

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Harto, it works consistantly for me. You should double check your path and filename.

    It displays rather than sends and the picture is there for me.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    VBAX Tutor
    Joined
    Sep 2007
    Posts
    265
    Location
    hi Steve.
    Ok then. perhaps it need a bit change on My PC setting.

    Have a great day!

  7. #7
    VBAX Newbie
    Joined
    Aug 2013
    Posts
    1
    Location
    Worked perfectrly!



    Quote Originally Posted by lucas View Post
    Try this Harto:
    Sub test()
    Dim OL As Object, rng As Range
    Dim EmailItem As Object
    Const mypath As String = "d:\My Pictures\"
    Application.ScreenUpdating = False
    Set OL = CreateObject("Outlook.Application")
    Set EmailItem = OL.CreateItem(0)
    ' EmailItem.Attachments.Add mypath & "AYANA-emailblast-100211.jpg"
    EmailItem.HTMLBody = "<html><p>This is a picture.</p>" & _
    "<img src=C:\Users\Steve\Documents\pictest.jpg' height=480 width=360>"
    EmailItem.Display
    End Sub
    Last edited by Aussiebear; 04-09-2023 at 04:46 PM. Reason: Adjusted the code tags

  8. #8
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    Quote Originally Posted by mac123 View Post
    Worked perfectrly!
    I afraid, the image is linked to your local file instead of embedded.
    In this case picture will not be displayed on recipient side (if it's not yours).
    Try this:
     Sub EmbedPicture()
      Const MyPath = "C:\Temp\"
      Const MyPicture = "My Picture.jpg"
      With CreateObject("Outlook.Application").CreateItem(0)
        .Attachments.Add MyPath & MyPicture
         .HTMLBody = "<html><p>This is a picture</p>" & _
                    "<img src=cid:" & Replace(MyPicture, " ", "%20") & " height=240 width=180>" & _
                     "<p>Best Regards,</p>" & _
                    "<p>" & UCase(Environ("USERNAME")) & "</p></html>"
        .Display
      End With
    End Sub

  9. #9

    MR

    Quote Originally Posted by ZVI View Post
    I afraid, the image is linked to your local file instead of embedded.
    In this case picture will not be displayed on recipient side (if it's not yours).
    Try this:
     Sub EmbedPicture()
      Const MyPath = "C:\Temp\"
      Const MyPicture = "My Picture.jpg"
      With CreateObject("Outlook.Application").CreateItem(0)
        .Attachments.Add MyPath & MyPicture
         .HTMLBody = "<html><p>This is a picture</p>" & _
                    "<img src=cid:" & Replace(MyPicture, " ", "%20") & " height=240 width=180>" & _
                     "<p>Best Regards,</p>" & _
                    "<p>" & UCase(Environ("USERNAME")) & "</p></html>"
        .Display
      End With
    End Sub

    "How can you make this dyncamic to ensure that it picks different images based on situation supplied"
    "Const MyPicture = "My Picture.jpg""

  10. #10
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    Quote Originally Posted by KIRAN.SHETTY View Post
    "How can you make this dyncamic to ensure that it picks different images based on situation supplied"
    "Const MyPicture = "My Picture.jpg""
    Hi and welcome to VbaExpress forum!
    This code displays file picker dialog to choose an image and embed it into email:
    Sub Main()  Dim PathName As String
      With Application.FileDialog(msoFileDialogFilePicker)
        '.InitialFileName = "D:\Temp\" ' <-- Use this line to set a default folder
        If .Filters.Count > 0 Then .Filters.Delete
        .Filters.Add "Images", "*.gif; *.png; *.jpg; *.jpeg", 1
        .AllowMultiSelect = False
        If .Show = False Then Exit Sub
        PathName = .SelectedItems(1)
      End With
      Call EmbedPicture(PathName)
    End Sub
    
    
    Sub EmbedPicture(PathName As String)
      Dim MyPicture As String
      MyPicture = Mid(PathName, InStrRev(PathName, Application.PathSeparator) + 1)
      With CreateObject("Outlook.Application").CreateItem(0)
        .Attachments.Add PathName
        .HTMLBody = "<html><p>This is a picture</p>" & _
                    "<img src=cid:" & Replace(MyPicture, " ", "%20") & _
                    "<p>Best Regards,</p>" & _
                    "<p>" & UCase(Environ("USERNAME")) & "</p></html>"
        .Display
      End With
    End Sub
    Regards

  11. #11
    Quote Originally Posted by ZVI View Post
    Hi and welcome to VbaExpress forum!
    This code displays file picker dialog to choose an image and embed it into email:
    Sub Main()  Dim PathName As String
      With Application.FileDialog(msoFileDialogFilePicker)
        '.InitialFileName = "D:\Temp\" ' <-- Use this line to set a default folder
        If .Filters.Count > 0 Then .Filters.Delete
        .Filters.Add "Images", "*.gif; *.png; *.jpg; *.jpeg", 1
        .AllowMultiSelect = False
        If .Show = False Then Exit Sub
        PathName = .SelectedItems(1)
      End With
      Call EmbedPicture(PathName)
    End Sub
    
    
    Sub EmbedPicture(PathName As String)
      Dim MyPicture As String
      MyPicture = Mid(PathName, InStrRev(PathName, Application.PathSeparator) + 1)
      With CreateObject("Outlook.Application").CreateItem(0)
        .Attachments.Add PathName
        .HTMLBody = "<html><p>This is a picture</p>" & _
                    "<img src=cid:" & Replace(MyPicture, " ", "%20") & _
                    "<p>Best Regards,</p>" & _
                    "<p>" & UCase(Environ("USERNAME")) & "</p></html>"
        .Display
      End With
    End Sub
    Regards
    "Thanks, It worked just as expected, Thanks again.

  12. #12
    Quote Originally Posted by ZVI View Post
    Hi and welcome to VbaExpress forum!
    This code displays file picker dialog to choose an image and embed it into email:
    Sub Main()  Dim PathName As String
      With Application.FileDialog(msoFileDialogFilePicker)
        '.InitialFileName = "D:\Temp\" ' <-- Use this line to set a default folder
        If .Filters.Count > 0 Then .Filters.Delete
        .Filters.Add "Images", "*.gif; *.png; *.jpg; *.jpeg", 1
        .AllowMultiSelect = False
        If .Show = False Then Exit Sub
        PathName = .SelectedItems(1)
      End With
      Call EmbedPicture(PathName)
    End Sub
    
    
    Sub EmbedPicture(PathName As String)
      Dim MyPicture As String
      MyPicture = Mid(PathName, InStrRev(PathName, Application.PathSeparator) + 1)
      With CreateObject("Outlook.Application").CreateItem(0)
        .Attachments.Add PathName
        .HTMLBody = "<html><p>This is a picture</p>" & _
                    "<img src=cid:" & Replace(MyPicture, " ", "%20") & _
                    "<p>Best Regards,</p>" & _
                    "<p>" & UCase(Environ("USERNAME")) & "</p></html>"
        .Display
      End With
    End Sub
    Regards
    "Sorry to be coming abck time and again, The code did work however when the email was being sent to external email id , the images never displayed on the body of the email and comes with an X mark.

    Is there a way to get this sorted"

Posting Permissions

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