Sending outlook msgs. with VB - image in body (changes bases on cell value)

countryfan_nt

Well-known Member
Joined
May 19, 2004
Messages
744
Hello friends, Hope all is well! Please help me make the code below work.
Trying to send Outlook mail via VBA, I wish to have the image appear in the body of the message. the image reads value from cell E1 of sheet db.

The draft doesn't show the image of which does exist in C drive.

Thank you very much in advance!

VBA Code:
Sub SENDMAIL()
 
Dim rng As Range

' Only send the visible cells in the selection.
LR = Sheets("db").Range("H1").Value

    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

'Adjust this according to your table gets the last row in the Sheet named "DB" column "A"
    ToAddress = Sheets("DB").Range("f1").Value
    FLOW = "C:\Users\nancy.taner\Downloads\avs\ssare\" & Worksheets("db").Range("E1").Value & ".png"

    On Error Resume Next
    With OutMail
        .To = ToAddress
        .CC = ccAddress
        .BCC = ""
        .Subject = "Thank you"
        .Attachments.Add FLOW, 1, 0
                                       
        'You can add other files also like this
        '.Attachments.Add (ActiveWorkbook.FullName)
   
      .Display                                        'or use .Display
    On Error GoTo 0

 ' Try to send
    On Error Resume Next

   'Set .SendUsingAccount = OutApp.Session.Accounts.Item("ms@drb.com")

    Set OutMail = Nothing
    Set OutApp = Nothing

strBody = ""

                   
            'Then we add an html <img src=''> link to this image
            'Note than you can customize width and height - not mandatory
                   
        .HTMLBody = "<html><p></p>" & _
                    "<img src=" & FLOW & "' height=520 width=750>"
                   
End With
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
20,828
Office Version
  1. 365
Platform
  1. Windows
After the line that adds the image as an attachment, add the following line . . .

VBA Code:
.HTMLBody = "<img src=""cid:" & Worksheets("db").Range("E1").Value & ".png"">"

Hope this helps!
 
Upvote 0

countryfan_nt

Well-known Member
Joined
May 19, 2004
Messages
744
After the line that adds the image as an attachment, add the following line . . .

VBA Code:
.HTMLBody = "<img src=""cid:" & Worksheets("db").Range("E1").Value & ".png"">"

Hope this helps!
Thank you so very much, it works like magic. May I ask for one last favor? Can you please resize both the height and width? Truly grateful
 
Upvote 0

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
20,828
Office Version
  1. 365
Platform
  1. Windows
Try . . .

VBA Code:
.HTMLBody = "<img src=""cid:" & Worksheets("db").Range("E1").Value & ".png""  width=""750"" height=""520"">"
 
Upvote 0

Forum statistics

Threads
1,186,807
Messages
5,959,912
Members
438,453
Latest member
NRG909

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
Top