VBA Code To Display Email Instead Of Automatically Send

I currently have a VBA code which selects a custom range and then emails said selected range to a list of E-mails I have in another worksheet. I've attached code, but this code is copied and pasted 8 times (there are 8 blocks)...Idk if that matters but just thought I should give as much information as possible. These are my two problems:

1) How to Display the email window on Outlook instead of automatically send (I've already tried .Item.Display and it does not work, so any alternatives or any other methods please recommend)
2) How to have the selected range keep its format (some of the text is red but once sent in an e-mail is displayed as default text).

Thanks in advance.

     For Each aCell In Worksheets("Email List").Range("B3:B" & Cells(Rows.Count, "B").End(xlUp).Row)
            If aCell <> "" Then
                    eTo = eTo & aCell & ";"
                End If
    Next
     eTo = Left(eTo, Len(eTo) - 1)
   If IsEmpty(Range("B4")) Then
   Else
      ActiveSheet.Range("a3", ActiveSheet.Range("e3").End(xlDown)).Select
      ActiveWorkbook.EnvelopeVisible = True
   With ActiveSheet.MailEnvelope

      .Item.To = eTo
      .Item.Subject = "Allocations -  Barclays" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If

June 28th, 2013 2:52pm

You would need to control Outlook directly instead of using the MailEnvelope method. See Ron deBruin's page here:

http://www.rondebruin.nl/win/s1/outlook/amail1.htm

That example displays the email instead of sending.....

Free Windows Admin Tool Kit Click here and download it now
June 28th, 2013 5:33pm

You would need to control Outlook directly instead of using the MailEnvelope method. See Ron deBruin's page here:

http://www.rondebruin.nl/win/s1/outlook/amail1.htm

That example displays the email instead of sending.....


Awesome, thank you for your reply! I've started to look into the link and was wondering if you could assist me in setting this up? I'm still pretty confused how I have to "re-work" this whole code. What would be the best way to go to about this. Thanks again, I really appreciate your help.
June 28th, 2013 6:08pm

Try this, but please change the body lines before actually using it:

            .Body = "Dearest person:" & Chr(10) & Chr(10)
            .Body = "Attached please find the Barclays allocations data." & Chr(10) & Chr(10)
            .Body = "Love, Alulla"

Sub Mail_Range()
    'Working in Excel 2000-2013
    'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim Source As Range
    Dim Dest As Workbook
    Dim wb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim OutApp As Object
    Dim OutMail As Object
    Dim aCell As Range

    Set Source = Nothing
    On Error Resume Next

    For Each aCell In Worksheets("Email List").Range("B3:B" & Cells(Rows.Count, "B").End(xlUp).Row)
        If aCell <> "" Then
            eTo = eTo & aCell & ";"
        End If
    Next
    eTo = Left(eTo, Len(eTo) - 1)

    If Not IsEmpty(Range("B4")) Then
        Set Source = ActiveSheet.Range("a3", ActiveSheet.Range("e3").End(xlDown))
    End If

    On Error GoTo 0

    If Source Is Nothing Then
        MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set wb = ActiveWorkbook
    Set Dest = Workbooks.Add(xlWBATWorksheet)

    Source.Copy
    With Dest.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial Paste:=xlPasteValues
        .Cells(1).PasteSpecial Paste:=xlPasteFormats
        .Cells(1).Select
        Application.CutCopyMode = False
    End With

    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Selection of " & wb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

    If Val(Application.Version) < 12 Then
        'You use Excel 97-2003
        FileExtStr = ".xls": FileFormatNum = -4143
    Else
        'You use Excel 2007-2013
        FileExtStr = ".xlsx": FileFormatNum = 51
    End If

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

    With Dest
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .to = eTo
            .CC = ""
            .BCC = ""
            .Subject = "Allocations -  Barclays" & Format(Date, " mm/dd/yyyy")
            .Body = "Dearest person:" & Chr(10) & Chr(10)
            .Body = "Attached please find the Barclays allocations data." & Chr(10) & Chr(10)
            .Body = "Love, Alulla"
            .Attachments.Add Dest.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display   'or use .Send
        End With
        On Error GoTo 0
        .Close savechanges:=False
    End With

    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub


Free Windows Admin Tool Kit Click here and download it now
June 28th, 2013 6:22pm

Try this, but please change the body lines before actually using it:

            .Body = "Dearest person:" & Chr(10) & Chr(10)
            .Body = "Attached please find the Barclays allocations data." & Chr(10) & Chr(10)
            .Body = "Love, Alulla"

Sub Mail_Range()
    'Working in Excel 2000-2013
    'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim Source As Range
    Dim Dest As Workbook
    Dim wb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim OutApp As Object
    Dim OutMail As Object
    Dim aCell As Range

    Set Source = Nothing
    On Error Resume Next

    For Each aCell In Worksheets("Email List").Range("B3:B" & Cells(Rows.Count, "B").End(xlUp).Row)
        If aCell <> "" Then
            eTo = eTo & aCell & ";"
        End If
    Next
    eTo = Left(eTo, Len(eTo) - 1)

    If Not IsEmpty(Range("B4")) Then
        Set Source = ActiveSheet.Range("a3", ActiveSheet.Range("e3").End(xlDown))
    End If

    On Error GoTo 0

    If Source Is Nothing Then
        MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set wb = ActiveWorkbook
    Set Dest = Workbooks.Add(xlWBATWorksheet)

    Source.Copy
    With Dest.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial Paste:=xlPasteValues
        .Cells(1).PasteSpecial Paste:=xlPasteFormats
        .Cells(1).Select
        Application.CutCopyMode = False
    End With

    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Selection of " & wb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

    If Val(Application.Version) < 12 Then
        'You use Excel 97-2003
        FileExtStr = ".xls": FileFormatNum = -4143
    Else
        'You use Excel 2007-2013
        FileExtStr = ".xlsx": FileFormatNum = 51
    End If

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

    With Dest
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .to = eTo
            .CC = ""
            .BCC = ""
            .Subject = "Allocations -  Barclays" & Format(Date, " mm/dd/yyyy")
            .Body = "Dearest person:" & Chr(10) & Chr(10)
            .Body = "Attached please find the Barclays allocations data." & Chr(10) & Chr(10)
            .Body = "Love, Alulla"
            .Attachments.Add Dest.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display   'or use .Send
        End With
        On Error GoTo 0
        .Close savechanges:=False
    End With

    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub



You're the best! This works great, but is there any way you can display the range inside the email instead of having it as an attachment??

June 28th, 2013 6:46pm

To mail your range in the body of an email, try this code (including the function at the bottom)

Sub Mail_Range_InBody()
    'Working in Excel 2000-2013
    'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim Source As Range
    Dim Dest As Workbook
    Dim wb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim OutApp As Object
    Dim OutMail As Object
    Dim aCell As Range

    Set Source = Nothing
    On Error Resume Next

    For Each aCell In Worksheets("Email List").Range("B3:B" & Cells(Rows.Count, "B").End(xlUp).Row)
        If aCell <> "" Then
            eTo = eTo & aCell & ";"
        End If
    Next
    eTo = Left(eTo, Len(eTo) - 1)

    If Not IsEmpty(Range("B4")) Then
        Set Source = ActiveSheet.Range("a3", ActiveSheet.Range("e3").End(xlDown))
    End If

    On Error GoTo 0

    If Source Is Nothing Then
        MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

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

    With Dest
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .to = eTo
            .CC = ""
            .BCC = ""
            .Subject = "Allocations -  Barclays" & Format(Date, " mm/dd/yyyy")
            .HTMLBody = RangetoHTML(Source)
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display   'or use .Send
        End With
        On Error GoTo 0
        .Close savechanges:=False
    End With

    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub



Function RangetoHTML(rng As Range)
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 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 workbook to receive the data.
    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 an .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 the RangetoHTML subroutine.
    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.
    Kill TempFile
 
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function

Free Windows Admin Tool Kit Click here and download it now
June 28th, 2013 8:47pm

To mail your range in the body of an email, try this code (including the function at the bottom)

Sub Mail_Range_InBody()
    'Working in Excel 2000-2013
    'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim Source As Range
    Dim Dest As Workbook
    Dim wb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim OutApp As Object
    Dim OutMail As Object
    Dim aCell As Range

    Set Source = Nothing
    On Error Resume Next

    For Each aCell In Worksheets("Email List").Range("B3:B" & Cells(Rows.Count, "B").End(xlUp).Row)
        If aCell <> "" Then
            eTo = eTo & aCell & ";"
        End If
    Next
    eTo = Left(eTo, Len(eTo) - 1)

    If Not IsEmpty(Range("B4")) Then
        Set Source = ActiveSheet.Range("a3", ActiveSheet.Range("e3").End(xlDown))
    End If

    On Error GoTo 0

    If Source Is Nothing Then
        MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

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

    With Dest
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .to = eTo
            .CC = ""
            .BCC = ""
            .Subject = "Allocations -  Barclays" & Format(Date, " mm/dd/yyyy")
            .HTMLBody = RangetoHTML(Source)
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display   'or use .Send
        End With
        On Error GoTo 0
        .Close savechanges:=False
    End With

    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub



Function RangetoHTML(rng As Range)
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 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 workbook to receive the data.
    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 an .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 the RangetoHTML subroutine.
    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.
    Kill TempFile
 
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function

Awesome! Thanks, how do I do the sub and then go onto the Function?

Also when I try and run the Sub it gives me a Run-time error '91': Object variable or With block variable not set error right here:

.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum

Thank you so much for your help!

June 28th, 2013 9:16pm

Ooops - sorry. I forgot to take that stuff out!  The function is called automatically from the Sub, so just run the Sub.

Use this version, cleaned up:

Sub Mail_Range_InBody()
    'Working in Excel 2000-2013
    'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim Source As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim aCell As Range
    Dim eTo As String

    Set Source = Nothing
    On Error Resume Next

    For Each aCell In Worksheets("Email List").Range("B3:B" & Cells(Rows.Count, "B").End(xlUp).Row)
        If aCell <> "" Then
            eTo = eTo & aCell & ";"
        End If
    Next
    eTo = Left(eTo, Len(eTo) - 1)

    If Not IsEmpty(Range("B4")) Then
        Set Source = ActiveSheet.Range("a3", ActiveSheet.Range("e3").End(xlDown))
    End If

    On Error GoTo 0

    If Source Is Nothing Then
        MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

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

    On Error Resume Next
    With OutMail
        .to = eTo
        .CC = ""
        .BCC = ""
        .Subject = "Allocations -  Barclays" & Format(Date, " mm/dd/yyyy")
        .HTMLBody = RangetoHTML(Source)
        'You can add other files also like this
        '.Attachments.Add ("C:\test.txt")
        .Display   'or use .Send
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub



Function RangetoHTML(rng As Range)
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 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 workbook to receive the data.
    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 an .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 the RangetoHTML subroutine.
    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.
    Kill TempFile
 
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function


Free Windows Admin Tool Kit Click here and download it now
June 28th, 2013 10:06pm

Ooops - sorry. I forgot to take that stuff out!  The function is called automatically from the Sub, so just run the Sub.

Use this version, cleaned up:

Sub Mail_Range_InBody()
    'Working in Excel 2000-2013
    'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim Source As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim aCell As Range
    Dim eTo As String

    Set Source = Nothing
    On Error Resume Next

    For Each aCell In Worksheets("Email List").Range("B3:B" & Cells(Rows.Count, "B").End(xlUp).Row)
        If aCell <> "" Then
            eTo = eTo & aCell & ";"
        End If
    Next
    eTo = Left(eTo, Len(eTo) - 1)

    If Not IsEmpty(Range("B4")) Then
        Set Source = ActiveSheet.Range("a3", ActiveSheet.Range("e3").End(xlDown))
    End If

    On Error GoTo 0

    If Source Is Nothing Then
        MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

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

    On Error Resume Next
    With OutMail
        .to = eTo
        .CC = ""
        .BCC = ""
        .Subject = "Allocations -  Barclays" & Format(Date, " mm/dd/yyyy")
        .HTMLBody = RangetoHTML(Source)
        'You can add other files also like this
        '.Attachments.Add ("C:\test.txt")
        .Display   'or use .Send
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub



Function RangetoHTML(rng As Range)
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 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 workbook to receive the data.
    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 an .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 the RangetoHTML subroutine.
    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.
    Kill TempFile
 
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function


Bernie,

Thank you so much! Never had such luck getting someone to help me to this extent. This works great! I have one last request, which I feel bad asking since you've done so much for me. But as you've presented the code for this block...this is just one table. I have eight other tables as well as different e-mail lists for both. Attached is the entire code I had for my Allocations thing, which I can now replace the first of the eight with since you have supplied me with Barclay's. How would I go about copying/pasting/editing the code you have supplied to work flawlessly throughout my entire body of work? Once again, thanks again. If theres any rewarding system on here please advise cause you've definitely already deserved it.

Sub Send_Range()
    Dim aCell As Range
    Dim eTo As String
    Dim bCell As Range
    Dim eTo1 As String
    Dim cCell As Range
    Dim eTo2 As String
    Dim dCell As Range
    Dim eTo3 As String
    Dim eCell As Range
    Dim eTo4 As String
    Dim fCell As Range
    Dim eTo5 As String
    Dim gCell As Range
    Dim eTo6 As String
    Dim hCell As Range
    Dim eTo7 As String
    Dim iCell As Range
    Dim eTo8 As String
    Dim jCell As Range
    Dim eTo9 As String
    Dim kCell As Range
    Dim eTo10 As String
    For Each aCell In Worksheets("Email List").Range("B3:B" & Cells(Rows.Count, "B").End(xlUp).Row)
            If aCell <> "" Then
                    eTo = eTo & aCell & ";"
                End If
    Next
     eTo = Left(eTo, Len(eTo) - 1)
   If IsEmpty(Range("B4")) Then
   Else
      ActiveSheet.Range("a3", ActiveSheet.Range("e3").End(xlDown)).Select
      ActiveWorkbook.EnvelopeVisible = True
   With ActiveSheet.MailEnvelope
    
      .Item.to = eTo
      .Item.Subject = "Allocations -  Barclays" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If
   
    For Each bCell In Worksheets("Email List").Range("E3:E" & Cells(Rows.Count, "E").End(xlUp).Row)
            If bCell <> "" Then
                    eTo1 = eTo1 & bCell & ";"
                End If
    Next
     eTo1 = Left(eTo1, Len(eTo1) - 1)
    
   If IsEmpty(Range("H4")) Then
   Else
        ActiveSheet.Range("G3", ActiveSheet.Range("K3").End(xlDown)).Select
    
    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.to = eTo1
      .Item.Subject = "Allocations - BNP" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If
   
   
   
    For Each cCell In Worksheets("Email List").Range("H3:H" & Cells(Rows.Count, "H").End(xlUp).Row)
            If cCell <> "" Then
                    eTo2 = eTo2 & cCell & ";"
                End If
    Next
     eTo2 = Left(eTo2, Len(eTo2) - 1)
   
           If IsEmpty(Range("N4")) Then
   Else
        ActiveSheet.Range("M3", ActiveSheet.Range("Q3").End(xlDown)).Select
    
    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.to = eTo2
      .Item.Subject = "Allocations - CITINY" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If



 For Each dCell In Worksheets("Email List").Range("K3:K" & Cells(Rows.Count, "E").End(xlUp).Row)
            If dCell <> "" Then
                    eTo3 = eTo3 & dCell & ";"
                End If
    Next
     eTo3 = Left(eTo3, Len(eTo3) - 1)

   If IsEmpty(Range("T4")) Then
   Else
        ActiveSheet.Range("S3", ActiveSheet.Range("W3").End(xlDown)).Select
    
    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.to = eTo3
      .Item.Subject = "Allocations - CSFB" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If
    
    
    
    
 For Each eCell In Worksheets("Email List").Range("N3:N" & Cells(Rows.Count, "E").End(xlUp).Row)
            If eCell <> "" Then
                    eTo4 = eTo4 & eCell & ";"
                End If
    Next
     eTo4 = Left(eTo4, Len(eTo4) - 1)
    
      If IsEmpty(Range("Z4")) Then
   Else
        ActiveSheet.Range("Y3", ActiveSheet.Range("AC3").End(xlDown)).Select
    
    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.to = eTo4
      .Item.Subject = "Allocations - DB" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If
   
   
   
 For Each fCell In Worksheets("Email List").Range("Q3:Q" & Cells(Rows.Count, "E").End(xlUp).Row)
            If fCell <> "" Then
                    eTo5 = eTo5 & fCell & ";"
                End If
    Next
     eTo5 = Left(eTo5, Len(eTo5) - 1)
      If IsEmpty(Range("AF4")) Then
   Else
        ActiveSheet.Range("AE3", ActiveSheet.Range("AI3").End(xlDown)).Select
    
    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.to = eTo5
      .Item.Subject = "Allocations - JPM" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If
   
   
 For Each gCell In Worksheets("Email List").Range("T3:T" & Cells(Rows.Count, "E").End(xlUp).Row)
            If gCell <> "" Then
                    eTo6 = eTo6 & gCell & ";"
                End If
    Next
     eTo6 = Left(eTo6, Len(eTo6) - 1)
   
      If IsEmpty(Range("AL4")) Then
   Else
        ActiveSheet.Range("AK3", ActiveSheet.Range("AO3").End(xlDown)).Select
    
    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.to = eTo6
      .Item.Subject = "Allocations - MS" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If
   
   
 For Each hCell In Worksheets("Email List").Range("W3:W" & Cells(Rows.Count, "E").End(xlUp).Row)
            If hCell <> "" Then
                    eTo7 = eTo7 & hCell & ";"
                End If
    Next
     eTo7 = Left(eTo7, Len(eTo7) - 1)
   
      If IsEmpty(Range("AR4")) Then
   Else
        ActiveSheet.Range("AQ3", ActiveSheet.Range("AU3").End(xlDown)).Select
    
    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.to = eTo7
      .Item.Subject = "Allocations - SABENY" & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If
   
    For Each iCell In Worksheets("Email List").Range("Z3:Z" & Cells(Rows.Count, "E").End(xlUp).Row)
            If iCell <> "" Then
                    eTo8 = eTo8 & iCell & ";"
                End If
    Next
  eTo8 = Left(eTo8, Len(eTo8) - 1)
   
      If IsEmpty(Range("AX4")) Then
   Else
        ActiveSheet.Range("AW3", ActiveSheet.Range("BA3").End(xlDown)).Select
    
    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.to = eTo8
      .Item.Subject = "Allocations - " & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If
   
    For Each jCell In Worksheets("Email List").Range("AC3:AC" & Cells(Rows.Count, "E").End(xlUp).Row)
            If jCell <> "" Then
                    eTo9 = eTo9 & jCell & ";"
                End If
    Next
     eTo9 = Left(eTo9, Len(eTo9) - 1)
   
      If IsEmpty(Range("BD4")) Then
   Else
        ActiveSheet.Range("BC3", ActiveSheet.Range("BG3").End(xlDown)).Select
    
    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.to = eTo9
      .Item.Subject = "Allocations - " & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If
   
    For Each kCell In Worksheets("Email List").Range("AF3:AF" & Cells(Rows.Count, "E").End(xlUp).Row)
            If kCell <> "" Then
                    eTo10 = eTo10 & kCell & ";"
                End If
    Next
     eTo10 = Left(eTo10, Len(eTo10) - 1)
   
      If IsEmpty(Range("BJ4")) Then
   Else
        ActiveSheet.Range("BI3", ActiveSheet.Range("BM3").End(xlDown)).Select
    
    ActiveWorkbook.EnvelopeVisible = True
      With ActiveSheet.MailEnvelope
      .Item.to = eTo10
      .Item.Subject = "Allocations - " & Format(Date, " mm/dd/yyyy")
      .Item.Send
   End With
   End If
End Sub

June 28th, 2013 10:35pm

You seem to have a very structured workbook, though I cannot find the custom value for the subject line for the last three sets of data.  

Anyway, try this version:

Sub Send_Range_Looping()
    Dim aCell As Range
    Dim i As Integer
    Dim rngB As Range
    Dim rngL As Range
    Dim Source As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim eTo(0 To 10) As String
    Dim varSL As Variant

    varSL = Array("Barclays", "BNP", "CITINY", "CSFB", "DB", "JPM", "MS", "SABENY", "", "", "")

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set OutApp = CreateObject("Outlook.Application")

    For i = 0 To 10
        With Worksheets("Email List")
            Set rngB = .Range("B3").Offset(0, i * 3)
            Set rngL = .Range(rngB, .Cells(.Rows.Count, rngB.Column).End(xlUp))
            For Each aCell In rngL
                If aCell <> "" Then
                    eTo(i) = eTo(i) & aCell & ";"
                End If
            Next aCell
            If Len(eTo(i)) > 0 Then eTo(i) = Left(eTo(i), Len(eTo(i)) - 1)
        End With

        Set rngB = ActiveSheet.Range("B4").Offset(0, i * 6)

        If Not IsEmpty(rngB) Then
            Set Source = ActiveSheet.Range(rngB.Offset(-1, -1), rngB.Offset(-1, 3).End(xlDown))
        End If
        
        Set OutMail = OutApp.CreateItem(0)

        On Error Resume Next
        With OutMail
            .to = eTo(i)
            .CC = ""
            .BCC = ""
            .Subject = "Allocations -  " & varSL(i) & Format(Date, " mm/dd/yyyy")
            .HTMLBody = RangetoHTML(Source)
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display   'or use .Send
        End With
        On Error GoTo 0

        Set OutMail = Nothing
    Next i
    
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

End Sub

Function RangetoHTML(rng As Range)
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 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 workbook to receive the data.
    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 an .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 the RangetoHTML subroutine.
    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.
    Kill TempFile
 
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function


Free Windows Admin Tool Kit Click here and download it now
June 28th, 2013 11:35pm

You seem to have a very structured workbook, though I cannot find the custom value for the subject line for the last three sets of data.  

Anyway, try this version:

Sub Send_Range_Looping()
    Dim aCell As Range
    Dim i As Integer
    Dim rngB As Range
    Dim rngL As Range
    Dim Source As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim eTo(0 To 10) As String
    Dim varSL As Variant

    varSL = Array("Barclays", "BNP", "CITINY", "CSFB", "DB", "JPM", "MS", "SABENY", "", "", "")

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set OutApp = CreateObject("Outlook.Application")

    For i = 0 To 10
        With Worksheets("Email List")
            Set rngB = .Range("B3").Offset(0, i * 3)
            Set rngL = .Range(rngB, .Cells(.Rows.Count, rngB.Column).End(xlUp))
            For Each aCell In rngL
                If aCell <> "" Then
                    eTo(i) = eTo(i) & aCell & ";"
                End If
            Next aCell
            If Len(eTo(i)) > 0 Then eTo(i) = Left(eTo(i), Len(eTo(i)) - 1)
        End With

        Set rngB = ActiveSheet.Range("B4").Offset(0, i * 6)

        If Not IsEmpty(rngB) Then
            Set Source = ActiveSheet.Range(rngB.Offset(-1, -1), rngB.Offset(-1, 3).End(xlDown))
        End If
        
        Set OutMail = OutApp.CreateItem(0)

        On Error Resume Next
        With OutMail
            .to = eTo(i)
            .CC = ""
            .BCC = ""
            .Subject = "Allocations -  " & varSL(i) & Format(Date, " mm/dd/yyyy")
            .HTMLBody = RangetoHTML(Source)
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display   'or use .Send
        End With
        On Error GoTo 0

        Set OutMail = Nothing
    Next i
    
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

End Sub

Function RangetoHTML(rng As Range)
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 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 workbook to receive the data.
    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 an .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 the RangetoHTML subroutine.
    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.
    Kill TempFile
 
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function



You're a god man. Thank you so much.
June 28th, 2013 11:50pm

You seem to have a very structured workbook, though I cannot find the custom value for the subject line for the last three sets of data.  

Anyway, try this version:

Sub Send_Range_Looping()
    Dim aCell As Range
    Dim i As Integer
    Dim rngB As Range
    Dim rngL As Range
    Dim Source As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim eTo(0 To 10) As String
    Dim varSL As Variant

    varSL = Array("Barclays", "BNP", "CITINY", "CSFB", "DB", "JPM", "MS", "SABENY", "", "", "")

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set OutApp = CreateObject("Outlook.Application")

    For i = 0 To 10
        With Worksheets("Email List")
            Set rngB = .Range("B3").Offset(0, i * 3)
            Set rngL = .Range(rngB, .Cells(.Rows.Count, rngB.Column).End(xlUp))
            For Each aCell In rngL
                If aCell <> "" Then
                    eTo(i) = eTo(i) & aCell & ";"
                End If
            Next aCell
            If Len(eTo(i)) > 0 Then eTo(i) = Left(eTo(i), Len(eTo(i)) - 1)
        End With

        Set rngB = ActiveSheet.Range("B4").Offset(0, i * 6)

        If Not IsEmpty(rngB) Then
            Set Source = ActiveSheet.Range(rngB.Offset(-1, -1), rngB.Offset(-1, 3).End(xlDown))
        End If
        
        Set OutMail = OutApp.CreateItem(0)

        On Error Resume Next
        With OutMail
            .to = eTo(i)
            .CC = ""
            .BCC = ""
            .Subject = "Allocations -  " & varSL(i) & Format(Date, " mm/dd/yyyy")
            .HTMLBody = RangetoHTML(Source)
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display   'or use .Send
        End With
        On Error GoTo 0

        Set OutMail = Nothing
    Next i
    
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

End Sub

Function RangetoHTML(rng As Range)
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 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 workbook to receive the data.
    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 an .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 the RangetoHTML subroutine.
    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.
    Kill TempFile
 
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function



Also, Bernie last question: If I didn't want the two emails popping up for the two "unmatched" emails at the end, how would I go about removing it? I don't want to mess up any of your beautiful code. Thanks again.
Free Windows Admin Tool Kit Click here and download it now
June 28th, 2013 11:56pm

Change the

for i = 0 to 10 

to

for i = 0 to 8  

(or 7)

June 28th, 2013 11:58pm

Change the

for i = 0 to 10 

to

for i = 0 to 8  

(or 7)

Bernie, thanks again for your help, that macro has been very successful and has been appreciated by me and my team. I wish there was a way to message you but I figured this would be the best alternative. I have another macro I've been working on that has been giving me trouble. If you have any knowledge of Case Select is there anyway you could take a look at this thread? Thanks.

http://social.technet.microsoft.com/Forums/office/en-US/608d1d92-34e6-43d9-b9c0-114ef1b80195/using-case-select-on-range-with-multiple-conditions

Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2013 11:48am

Change the

for i = 0 to 10 

to

for i = 0 to 8  

(or 7)

Hey Bernie,

I don't know if you remember this post/how you answered it. But I've been running into problems lately using the macro you helped create for me. The code is still on this thread so it's easy for you to view. But here is what the problem is:

As you know, I currently have a VBA code which selects a custom range and then emails said selected range to a list of E-mails I have in another worksheet. The only problem is that I want it to email the custom range to the said list ONLY IF the custom range is actually a table. Like in the old code which is in the first post which we completely changed, I made it so it would check if something was in B4 for the first table, H4 for the second table, and so on.

What is happening with the code you've helped me piece together is, it will automatically send blank emails to the wrong email list (because the order gets messed up) if one of the custom ranges is in fact empty). Please let me know if you need me to clarify this any further, and if there is anyway you can help me out. Thank you so much!

August 5th, 2013 5:26pm

Change the

for i = 0 to 10 

to

for i = 0 to 8  

(or 7)


Hey Bernie, sorry for annoying you. Not sure if you saw my last quote of your message for help. Any assistance you can provide, would be greatly appreciated!
Free Windows Admin Tool Kit Click here and download it now
August 6th, 2013 2:13pm

Try replacing

If Not IsEmpty(rngB) Then
            Set Source = ActiveSheet.Range(rngB.Offset(-1, -1), rngB.Offset(-1, 3).End(xlDown))
        End If
        
        Set OutMail = OutApp.CreateItem(0)

        On Error Resume Next
        With OutMail
            .to = eTo(i)
            .CC = ""
            .BCC = ""
            .Subject = "Allocations -  " & varSL(i) & Format(Date, " mm/dd/yyyy")
            .HTMLBody = RangetoHTML(Source)
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display   'or use .Send
        End With



with



    If Not IsEmpty(rngB) Then
        Set Source = ActiveSheet.Range(rngB.Offset(-1, -1), rngB.Offset(-1, 3).End(xlDown))

        MsgBox "Count of values is " & Application.CountA(Source)

        'If Application.CountA(Source) > XXX Then
            Set OutMail = OutApp.CreateItem(0)

            On Error Resume Next
            With OutMail
                .to = eTo(i)
                .CC = ""
                .BCC = ""
                .Subject = "Allocations -  " & varSL(i) & Format(Date, " mm/dd/yyyy")
                .HTMLBody = RangetoHTML(Source)
                'You can add other files also like this
                '.Attachments.Add ("C:\test.txt")
                .Display   'or use .Send
            End With
       ' End If
    End If

Then, run the code a few times, and figure out what values shown in the message box

        MsgBox "Count of values is " & Application.CountA(Source)

will indicate that you want to send the email.  Then change the XXX to that value, then uncomment the line

        'If Application.CountA(Source) > XXX Then

and its corresponding End If

August 6th, 2013 3:44pm

Try replacing

If Not IsEmpty(rngB) Then
            Set Source = ActiveSheet.Range(rngB.Offset(-1, -1), rngB.Offset(-1, 3).End(xlDown))
        End If
        
        Set OutMail = OutApp.CreateItem(0)

        On Error Resume Next
        With OutMail
            .to = eTo(i)
            .CC = ""
            .BCC = ""
            .Subject = "Allocations -  " & varSL(i) & Format(Date, " mm/dd/yyyy")
            .HTMLBody = RangetoHTML(Source)
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display   'or use .Send
        End With



with



    If Not IsEmpty(rngB) Then
        Set Source = ActiveSheet.Range(rngB.Offset(-1, -1), rngB.Offset(-1, 3).End(xlDown))

        MsgBox "Count of values is " & Application.CountA(Source)

        'If Application.CountA(Source) > XXX Then
            Set OutMail = OutApp.CreateItem(0)

            On Error Resume Next
            With OutMail
                .to = eTo(i)
                .CC = ""
                .BCC = ""
                .Subject = "Allocations -  " & varSL(i) & Format(Date, " mm/dd/yyyy")
                .HTMLBody = RangetoHTML(Source)
                'You can add other files also like this
                '.Attachments.Add ("C:\test.txt")
                .Display   'or use .Send
            End With
       ' End If
    End If

Then, run the code a few times, and figure out what values shown in the message box

        MsgBox "Count of values is " & Application.CountA(Source)

will indicate that you want to send the email.  Then change the XXX to that value, then uncomment the line

        'If Application.CountA(Source) > XXX Then

and its corresponding End If


Thanks for the reply! I followed your steps and when running get the following numbers: 130, 116, 94, 90, & 36. The thing is the tables that don't have anything in them (which the error was producing .Display emails for) did not even return a number and didn't show .Display this time). So I guess it's working? I'm confused on the XXX Step and would like to know what you exactly meant with that. Other than that, it seems that it's working fine just need to get rid of the "Count of values". I'll wait for your reply for further assistance. Thanks again man, you're a hero.
Free Windows Admin Tool Kit Click here and download it now
August 6th, 2013 4:51pm

rngB was nothing, apparently, so the CountA was not needed. Try this:

 If Not IsEmpty(rngB) Then
        Set Source = ActiveSheet.Range(rngB.Offset(-1, -1), rngB.Offset(-1, 3).End(xlDown))

           Set OutMail = OutApp.CreateItem(0)

            On Error Resume Next
            With OutMail
                .to = eTo(i)
                .CC = ""
                .BCC = ""
                .Subject = "Allocations -  " & varSL(i) & Format(Date, " mm/dd/yyyy")
                .HTMLBody = RangetoHTML(Source)
                'You can add other files also like this
                '.Attachments.Add ("C:\test.txt")
                .Display   'or use .Send
            End With
    End If

August 6th, 2013 5:27pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics