How to send email from excel using VBA with Cell Range (Including Images) as Email Body

raghuram.star

Board Regular
Joined
Sep 5, 2012
Messages
102
I want to send email from excel using VBA with Cell Range (Including Images) as Email Body, I have below code to send email as HTML body every this is coming to email body (formats & fonts) but Images are not displayed in email body. getting error "The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted"

Code:
Sub Send_eMail()
    Call eMailRangeAsBody(WksQuote.Range("D18").Value, "Quote : eMail", "B2:N61")
End Sub


Public Sub eMailRangeAsBody(strTo As String, strSubject As String, rRange As String)
    
    Dim oBook As Excel.Workbook      ' Excel workbook
    Dim oSheet As Excel.Worksheet     ' Excel Worksheet
    
    Dim oOutlookApp As Object 'New Outlook.Application
    Dim oOutlookMessage As Object
    Dim oFSObj As Object
    Dim oFSTextStream As Object
    Dim rngeSend As Range
    Dim strHTMLBody As String
    Dim strTempFilePath As String
    
    Set oBook = ThisWorkbook
    Set oSheet = oBook.Worksheets(1)
    
    On Error Resume Next
    Set rngeSend = oSheet.Range(rRange)
    If rngeSend Is Nothing Then Exit Sub
    On Error GoTo 0
    
    Set oFSObj = CreateObject("Scripting.FilesystemObject")
    strTempFilePath = oFSObj.GetSpecialFolder(2)
    strTempFilePath = strTempFilePath & "\XLRange.htm"
    
    oBook.PublishObjects.Add(4, strTempFilePath, _
        oSheet.Name, rRange, 0, "", "").Publish True
    
    Set oOutlookApp = CreateObject("Outlook.Application")
    
    Set oOutlookMessage = oOutlookApp.CreateItem(0)
    
    Set oFSTextStream = oFSObj.OpenTextFile(strTempFilePath, 1)
    
    strHTMLBody = oFSTextStream.ReadAll
    
    strHTMLBody = Replace(strHTMLBody, "align=center", "align=left", _
        , , vbTextCompare)
    
    oOutlookMessage.HTMLBody = strHTMLBody
    oOutlookMessage.HTMLBody = strHTMLBody
    oOutlookMessage.To = strTo
    oOutlookMessage.Subject = strSubject
    
        'Attach images to email
        txtFpath = "C:\Images\"
        Pathf = fs.GetFolder(txtFpath)
        Set MyObject = New Scripting.FileSystemObject
        Set mySource = MyObject.GetFolder(Pathf)
        On Error Resume Next
        iCol = 4
        For Each myFile In mySource.Files
            If Left(myFile.Name, 14) = WksQuote.Range("I8").Value Then 'Attach all Quote related images
                FilePath = myFile
                oOutlookMessage.Attachments.Add (FilePath)
            End If
        Next


    oOutlookMessage.Display
    
    Call DeleteFile(strTempFilePath)
    
    Set oBook = Nothing
    Set oFSTextStream = Nothing
    Set oOutlookMessage = Nothing
    Set oOutlookApp = Nothing
    Set oFSObj = Nothing
End Sub
Some one please help me
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Forum statistics

Threads
1,214,878
Messages
6,122,062
Members
449,064
Latest member
scottdog129

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