• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Excel to outlook vba

mehman

New Member
Hi,

I would like to find an approach to do the following:
  1. Copy data from range B18:F20 as a table in the email with the numbers in bold
  2. Copy the range B2:G13 as image in outlook email.
  3. The items have to be in spcefic order, first the table(point 1) and then the range as image(point2)
I have attached a sample file. The ranges mentioned above are as per the attached file.

Thanks in advance.

Mandeep
 

Attachments

  • Book1.xlsx
    240.4 KB · Views: 2
.
Paste this into a Routine Module attached to Command Button:

Code:
Option Explicit


Sub SendEmail()
    Dim rng As Range, OutApp As Object, OutMail As Object
    Dim sCC As String, sSubj As String, sEmAdd As String
    Dim objCombinedR As Range
     '// Change the values of these variables to suit
    sEmAdd = "abc@abc.com"
    sCC = ""
    sSubj = "My Subject"
    
    Set rng = Nothing
    On Error Resume Next
  
    Sheets("Sheet1").Range("Q2:Y4") = Sheets("Sheet1").Range("B18:F20").Value
    Sheets("Sheet1").Range("Q7:V17") = Sheets("Sheet1").Range("B2:G13").Value
  
    Set rng = Sheets("Sheet1").Range("Q2:V17")
    rng.AutoFit
  
    On Error GoTo 0
    
    With Application
        .EnableEvents = 0
        .ScreenUpdating = 0
        .Calculation = xlCalculationManual
    End With
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
    On Error Resume Next
    With OutMail
        .To = sEmAdd
        .CC = sCC
        .Subject = sSubj
        .HTMLBody = "<p>Dear Name:" & "<br><br>" & _
                "Please see attached for your review.." & "<br><br>" & _
                RangetoHTML(rng) & "<br><br>" & _
                "Regards," & "<br><br>" & _
                "Finance</p>"
        '.Send '// Change this to .Display if you want to view the email before sending.
        .Display
    End With
    On Error GoTo 0
    
    With Application
        .EnableEvents = 1
        .Calculation = xlCalculationAutomatic
    End With
    Sheets("Sheet1").Range("Q2:V17").Value = ""
    Set OutMail = Nothing: Set OutApp = Nothing
    
End Sub

Function RangetoHTML(rng As Range)
    Dim fso As Object, ts As Object, TempWB As Workbook, TempFile As String
    
    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
    
    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 xlPasteColumnWidths, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
    End With
    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
    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=")
    
    TempWB.Close 0
    Kill TempFile
    
    Set ts = Nothing: Set fso = Nothing: Set TempWB = Nothing
    
End Function

Presently the code utilizes Sheets("Sheet1").Range("Q2:V17") as a temporary location to paste the data for proper presentation in the email. This range is cleared after the email is created. The range can be changed to any unused area you decide.
 

Attachments

  • Email Forecast.xlsm
    271.5 KB · Views: 4
.
This version may be more to your liking :
 

Attachments

  • Email Forecast 2.xlsm
    272.9 KB · Views: 10
Back
Top