Paste Image to Body of Email

Johndinho

Board Regular
Joined
Mar 21, 2013
Messages
90
Hi,

I am trying to paste an image to the body of an email and getting a strange error

Code:
Sub SendEmail()

Dim oWSHShell As Object
Set oWSHShell = CreateObject("WScript.Shell")
Dim Msg As Object
Dim Conf As Object
Dim msgBody As String
Dim ConfFields As Variant
Dim wb As Workbook

With Application
.EnableEvents = False
End With

Set wb = ActiveWorkbook
Set Msg = CreateObject("CDO.Message")
Set Conf = CreateObject("CDO.Configuration")

Conf.Load -1    ' CDO Source Defaults
Set ConfFields = Conf.Fields
    With ConfFields
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    'Enter the username and password of your email account below
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "me@gmail.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
    'Edit the SMTP server below e.g. smtp.gmail.com or smtp.mail.yahoo.co.uk
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
    .Update
End With
With Msg
    Set .Configuration = Conf
    'Add the email address to whom to send the email below
    .To = "me@gmail.com"
    .CC = ""
    .BCC = ""
    .From = "someone@gmail"
    .Subject = "Image"
    .HTMLBody = "< img src='oWSHShell.SpecialFolders("Desktop") & "\DieBond.png"'>"     (Without the space between "<" and "img")
    .Send
End With

End Sub

I have to use SpecialFolders because different users have their desktops in different places (Thanks to our Dropbox installer: c:\users\username\desktop or c:\desktop)

I can export the files to the desktop no problem but everytime I try to run the code I get;

Compile Error:
Expected: end of statement

And the .HTMLBody line is highlighted.

I use this code elsewhere and the "Call .AddAttchment" function but for this project; I want the image in the body of the email

Any suggestions would be gratefully received.

In your debt as always
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try this

Code:
Sub SendEmail()


Dim oWSHShell As Object
Set oWSHShell = CreateObject("WScript.Shell")
Dim Msg As Object
Dim Conf As Object
Dim msgBody As String
Dim ConfFields As Variant
Dim wb As Workbook
Dim Path As String


With Application
.EnableEvents = False
End With


Set wb = ActiveWorkbook
Set Msg = CreateObject("CDO.Message")
Set Conf = CreateObject("CDO.Configuration")
Path = oWSHShell.SpecialFolders("Desktop")


Conf.Load -1    ' CDO Source Defaults
Set ConfFields = Conf.Fields
    With ConfFields
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    'Enter the username and password of your email account below
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "me@gmail.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
    'Edit the SMTP server below e.g. smtp.gmail.com or smtp.mail.yahoo.co.uk
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
    .Update
End With
With Msg
    Set .Configuration = Conf
    .attachments.Add Path & "\" & "DieBond.png", olbyvalue, 0
    'Add the email address to whom to send the email below
    .To = "me@gmail.com"
    .CC = ""
    .BCC = ""
    .From = "someone@gmail"
    .Subject = "Image"
    .HTMLBody = "< img src='cid:DieBond.png'>" 'Remove space
    .Send
End With


End Sub
 
Upvote 0
Thanks but I got a different error;

Run-time error '450';
Wring number of arguments or invalid property assignment

And it debugs to

.attachments.Add Path & "" & "DieBond.png", olbyvalue, 0
 
Upvote 0
Try

.attachments.Add Path & "" & "DieBond.png", Empty, 0
 
Last edited:
Upvote 0
When I post it is getting rid of the \ character in between the "".

Make sure to put the \ after the 'Path' variable
 
Upvote 0
Got it!! this should be the last part of your code

Code:
With Msg
    Set .Configuration = Conf
    Call .Addattachment(oWSHShell.SpecialFolders("Desktop") & "\" & "Image.png")
    'Add the email address to whom to send the email below
    .To = "me@gmail.nl"
    .CC = ""
    .BCC = ""
    .From = "someone@gmail.com"
    .Subject = "Image"
    .HTMLBody = "< img src='cid:Image.png'>" 'Remove space
    .Send
End With

Thanks Robbo
 
Upvote 0
Solution
Not sureif your other suggestions will work; I'm going to try adding more than one image now so I may be back!

Thanks again
 
Upvote 0
Cool. Glad you got it worked out.

───────────────────░█▓▓▓█░▇▆▅▄▃▂
──────────────────░█▓▓▓▓▓█░▇▆▅▄▃▂
─────────────────░█▓▓▓▓▓█░▇▆▅▄▃▂
──────────░░░───░█▓▓▓▓▓▓█░▇▆▅▄▃▂ ...
─────────░███░──░█▓▓▓▓▓█░▇▆▅▄▃▂
───────░██░░░██░█▓▓▓▓▓█░▇▆▅▄▃▂
──────░█░░█░░░░██▓▓▓▓▓█░▇▆▅▄▃▂
────░██░░█░░░░░░█▓▓▓▓█░▇▆▅▄▃▂
───░█░░░█░░░░░░░██▓▓▓█░▇▆▅▄▃▂
──░█░░░░█░░░░░░░░█▓▓▓█░▇▆▅▄▃▂
──░█░░░░░█░░░░░░░░█▓▓▓█░▇▆▅▄▃▂
──░█░░█░░░█░░░░░░░░█▓▓█░▇▆▅▄▃▂
─░█░░░█░░░░██░░░░░░█▓▓█░▇▆▅▄▃▂
─░█░░░░█░░░░░██░░░█▓▓▓█░▇▆▅▄▃▂
─░█░█░░░█░░░░░░███▓▓▓▓█░▇▆▅▄▃▂
░█░░░█░░░██░░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
░█░░░░█░░░░█████▓▓▓▓▓█░▇▆▅▄▃▂
░█░░░░░█░░░░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
░█░█░░░░██░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
─░█░█░░░░░████▓▓▓▓██░▇▆▅▄▃▂
─░█░░█░░░░░░░█▓▓██▓█░▇▆▅▄▃▂
──░█░░██░░░██▓▓█▓▓▓█░▇▆▅▄▃▂
───░██░░███▓▓██▓█▓▓█░▇▆▅▄▃▂
────░██▓▓▓███▓▓▓█▓▓▓█░▇▆▅▄▃▂
──────░█▓▓▓▓▓▓▓▓█▓▓▓█░▇▆▅▄▃▂
──────░█▓▓▓▓▓▓▓▓▓▓▓▓▓█░▇▆▅▄▃▂
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top