+ Reply to Thread
Results 1 to 5 of 5

sending Email (via outlook) from Excel with cell range and image from external file

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    sending Email (via outlook) from Excel with cell range and image from external file

    Hi,

    I've been looking to send a cell range from excel via outlook email, which I have managed successfully with the vba code I found below. However I would like to embed or have added a picture in a similar fashion to the insert--picture route. The picture is currently located in a network folder s:\headers.

    I've spent a little while searching for a solution but the answeres I found went miles over my head.

    Sub Mail_Selection_Range_Outlook_Body()
    ' Don't forget to copy the function RangetoHTML in the module.
    ' Working in Office 2000-2010
        Dim rng As Range
        Dim OutApp As Object
        Dim OutMail As Object
        Dim StrBody As String
    
        StrBody = "<br><br><br>"
    
        Set rng = Nothing
        On Error Resume Next
        'Only the visible cells in the selection
        Set rng = Sheets("Order").Range("A15:F26").SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
    
        If rng Is Nothing Then
            MsgBox "The selection is not a range or the sheet is protected" & _
                   vbNewLine & "please correct and try again.", vbOKOnly
            Exit Sub
        End If
    
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        On Error Resume Next
        With OutMail
            '.To = "ron@debruin.nl"
            '.CC = ""
            '.BCC = ""
            .Subject = "TEST"
            .HTMLBody = StrBody & RangetoHTML(rng)
            .Display
        End With
        On Error GoTo 0
    
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub
    Function RangetoHTML(rng As Range)
    ' Changed by Ron de Bruin 28-Oct-2006
    ' Working in Office 2000-2010
        Dim fso As Object
        Dim ts As Object
        Dim TempFile As String
        Dim TempWB As Workbook
     
        TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
     
        'Copy the range and create a new workbook to past the data in
        rng.Copy
        Set TempWB = Workbooks.Add(1)
        With TempWB.Sheets(1)
            .Cells(1).PasteSpecial Paste:=8
            .Cells(1).PasteSpecial xlPasteValues, , False, False
            .Cells(1).PasteSpecial xlPasteFormats, , False, False
            .Cells(1).Select
            Application.CutCopyMode = False
            On Error Resume Next
            .DrawingObjects.Visible = True
            .DrawingObjects.Delete
            On Error GoTo 0
        End With
     
        'Publish the sheet to a htm file
        With TempWB.PublishObjects.Add( _
             SourceType:=xlSourceRange, _
             FileName:=TempFile, _
             Sheet:=TempWB.Sheets(1).Name, _
             Source:=TempWB.Sheets(1).UsedRange.Address, _
             HtmlType:=xlHtmlStatic)
            .Publish (True)
        End With
     
        'Read all data from the htm file into RangetoHTML
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
        RangetoHTML = ts.ReadAll
        ts.Close
        RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                              "align=left x:publishsource=")
     
        'Close TempWB
        TempWB.Close savechanges:=False
     
        'Delete the htm file we used in this function
        Kill TempFile
     
        Set ts = Nothing
        Set fso = Nothing
        Set TempWB = Nothing
    End Function

  2. #2
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: sending Email (via outlook) from Excel with cell range and image from external file

    This works for me (Excel & Outlook 2003).
    Sub Mail_Selection_Range_Outlook_Body()
    ' Don't forget to copy the function RangetoHTML in the module.
    ' Working in Office 2000-2010
        Dim rng As Range
        Dim OutApp As Object
        Dim OutMail As Object
        Dim imageFolder As String, imageFileName As String
        Dim HTML As String, p As Long
    
        'Folder containing image to be embedded and the file name of the image - CHANGE AS REQUIRED
        
        imageFolder = "S:\headers\"
        imageFileName = "picture.jpg"
    
        Set rng = Nothing
        On Error Resume Next
        'Only the visible cells in the selection
        Set rng = Sheets("Order").Range("A15:F26").SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
    
        If rng Is Nothing Then
            MsgBox "The selection is not a range or the sheet is protected" & _
                   vbNewLine & "please correct and try again.", vbOKOnly
            Exit Sub
        End If
    
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        
        'Convert range of cells to HTML string
        
        HTML = RangetoHTML(rng)
        
        'Find HTML body closing tag and insert embedded image reference before it
        
        p = InStr(HTML, "</body>")
        HTML = Left(HTML, p - 1) & "<p>The embedded image is shown below.</p><img src='cid:" & imageFileName & "'>" & Mid(HTML, p)
        
        'On Error Resume Next
        With OutMail
            .To = "email.address@domain.com"            'CHANGE AS REQUIRED
            .CC = ""
            .BCC = ""
            .Subject = "TEST " & Now
            .Attachments.Add imageFolder & imageFileName
            .HTMLBody = HTML
            .Display                'Image embedded in email body
            '.Send                  'Image appears as an attachment
        End With
        'On Error GoTo 0
    
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub
    The RangeToHTML function is the same as in your post.

    As noted in the comment in the code, if you use Send to send the email immediately instead of Display, the image appears as a file attachment rather than an embedded image. If you prefer Send and having the image properly embedded, try the code at http://www.outlookcode.com/d/code/htmlimg.htm, combined with the string manipulation in my code which inserts the image tag in a suitable place in the HTML string.

  3. #3
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Re: sending Email (via outlook) from Excel with cell range and image from external file

    wow that simple eh?

    That was so helpful thank you!
    Last edited by Kramxel; 04-19-2012 at 06:52 AM.

  4. #4
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: sending Email (via outlook) from Excel with cell range and image from external file

    Your initial reply, now edited, asked how to put the image at the top of the email rather than the bottom. Maybe you've worked it out, but for the benefit of others here's how.

    Replace:
        'Find HTML body closing tag and insert embedded image reference before it
        
        p = InStr(HTML, "</body>")
        HTML = Left(HTML, p - 1) & "<p>The embedded image is shown below.</p><img src='cid:" & imageFileName & "'>" & Mid(HTML, p)
    with:
        'Find HTML body opening tag and insert embedded image reference after it
        
        p = InStr(HTML, "<body>") + Len("<body>")
        HTML = Left(HTML, p) & "<p>The embedded image is shown below.</p><img src='cid:" & imageFileName & "'>" & Mid(HTML, p)
    Post responsibly. Search for excelforum.com

  5. #5
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Re: sending Email (via outlook) from Excel with cell range and image from external file

    Hi Chippy,

    Yes I did manage to work it out realised like most things the solution was far easier than I was expecting so quickly removed it (well not as quickly as I had hoped).

    once again thank you so much.

    althogh I didn't add the + Len("<body>") O just took the / away... i'll add that now and see what the difference is.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1