Solved Inserting an image into an email (1 Viewer)

MisterChips

New member
Local time
Today, 16:10
Joined
Oct 13, 2022
Messages
11
I have managed to achieve the above as long as I use an actual path (Please excuse me if my terminology isn't exact).
However, If I try to use a string, I cannot get it to work.
I have tried as many combinations of " and "" and '. as I can think of, without luck.
This is the code I have been using:-

Private Sub Command13_Click()
Dim strEmailAddress As String
Dim appOutlook As Outlook.Application: Set appOutlook = New Outlook.Application
Dim mimEmail As Outlook.MailItem
Dim strImageFullPath
Dim att As Outlook.Attachment
Set mimEmail = appOutlook.CreateItem(olMailItem)
strEmailAddress = "dave@lllllll.co.uk"
strImageFullPath = "C:\PGCO\Supporting_the_2025_Festival.jpg"
Set att = mimEmail.Attachments.Add(strImageFullPath, 1, 0)
With mimEmail
.To = strEmailAddress
.Subject = "2025 Festival"
' #####################
'.HTMLBody = "<img src=""C:\PGCO\Supporting_the_2025_Festival.jpg""></html>" ' WHY DOES THIS WORK
.HTMLBody = "<img src=strImageFullPath></html>" ' WHILST THIS DOESN'T?
' #####################
.Display
End With
End Sub

I have also added the above as a file.

What syntax do I need to make the lower .HTMLBody line work?

Any advice that can be given will be well received.
 

Attachments

  • Emailing head test.accdb
    580 KB · Views: 84

Gasman

Enthusiastic Amateur
Local time
Today, 16:10
Joined
Sep 21, 2011
Messages
14,311
You would need to concatenate the variable strImageFullPath with the hard coded text.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:10
Joined
Oct 29, 2018
Messages
21,474
Hi. Welcome to AWF!

Try it this way:
Code:
.HTMLBody = "<img src=""" & strImageFullPath & """></html>"
Hope that helps...
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:10
Joined
Sep 21, 2011
Messages
14,311
To get EXACTLY what you say works, you will likely need
Code:
? "<img src=""" & chr(34) & strImageFullPath & chr(34) & """></html>"
<img src=""c:\temp\data\twenty""></html>

I could not get the number of " correct, so just took the easy way out. :)
 

MisterChips

New member
Local time
Today, 16:10
Joined
Oct 13, 2022
Messages
11
It works perfectly. I am most grateful to you.
I was never anywhere near the solution you provided.
Now I have to try and understand why it works!
Agin, my sincere thanks, especially for your instant reply.
 

plog

Banishment Pending
Local time
Today, 10:10
Joined
May 11, 2011
Messages
11,646
Code:
'.HTMLBody = "<img src=""C:\PGCO\Supporting_the_2025_Festival.jpg""></html>" ' WHY DOES THIS WORK
.HTMLBody = "<img src=strImageFullPath></html>" ' WHILST THIS DOESN'T?

1. It doesn't work because you do not include quote marks around the variables value, but you did include them when you hard coded the value.

2. Neither will work if the email is going to someone who's C drive isn't your C drive. You are not including the image in that email, you are sending a reference in the email of where to find the image file. The recipients email client is going to look into whatever their C drive is and most likely not find that image. That image most likely needs to go on a web server.
 

isladogs

MVP / VIP
Local time
Today, 16:10
Joined
Jan 14, 2017
Messages
18,236
For info, even if you can send images successfully, many users will block inline images by default & some email accounts will block them automatically.

If the images are important, it is safer to include them as attachments if that isn't already your intention
 

MisterChips

New member
Local time
Today, 16:10
Joined
Oct 13, 2022
Messages
11
For info, even if you can send images successfully, many users will block inline images by default & some email accounts will block them automatically.

If the images are important, it is safer to include them as attachments if that isn't already your intention
That is excellent advice. . . Thank You.
 

Users who are viewing this thread

Top Bottom