Sub Auto_Open()
Run "Mail_Sheet_Outlook_Body"
End Sub
Sub Mail_Sheet_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
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set rng = Nothing
Set rng = Sheets("Sheet3").UsedRange
'You can also use a sheet name
'Set rng = Sheets("YourSheet").UsedRange
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = Range("a2") & ";" & Range("a3") & ";" & Range("a4") & ";" & Range("a5") & ";" & Range("a6") & ";" & Range("a7") & ";" & Range("a8") & ";" & Range("a9") & ";" & Range("a10") & ";" & Range("a11") & ";" & Range("a12") & ";" & Range("a13") & ";" & Range("a14") & ";" & Range("a15") & ";" & Range("a16") & ";" & Range("a17") & ";" & Range("a18") & ";" & Range("a19") & ";" & Range("a20") & ";" & Range("a21") & ";" & Range("a22") & ";" & Range("a23") & ";" & Range("a24") & ";" & Range("a25") & ";" & Range("a26") & ";" & Range("a27") & ";" & Range("a28") & ";" & Range("a29") & ";" & Range("a30") & ";"
.CC = Range("d2") & ";" & Range("d3") & ";" & Range("d4") & ";" & Range("d5") & ";" & Range("d6") & ";" & Range("d7") & ";" & Range("d8") & ";" & Range("d9") & ";" & Range("d10") & ";" & Range("d11") & ";" & Range("d12") & ";" & Range("d13") & ";" & Range("d14") & ";" & Range("d15") & ";" & Range("d16") & ";" & Range("d17") & ";" & Range("d18") & ";" & Range("d19") & ";" & Range("d20") & ";" & Range("d21") & ";" & Range("d22") & ";" & Range("d23") & ";" & Range("d24") & ";" & Range("d25") & ";" & Range("d26") & ";" & Range("d27") & ";" & Range("d28") & ";" & Range("d29") & ";" & Range("d30") & ";"
.BCC = Range("e2") & ";" & Range("e3") & ";" & Range("e4") & ";" & Range("e5") & ";" & Range("e6") & ";" & Range("e7") & ";" & Range("e8") & ";" & Range("e9") & ";" & Range("e10") & ";" & Range("e11") & ";" & Range("e12") & ";" & Range("e13") & ";" & Range("e14") & ";" & Range("e15") & ";" & Range("e16") & ";" & Range("e17") & ";" & Range("e18") & ";" & Range("e19") & ";" & Range("e20") & ";" & Range("e21") & ";" & Range("e22") & ";" & Range("e23") & ";" & Range("e24") & ";" & Range("e25") & ";" & Range("e26") & ";" & Range("e27") & ";" & Range("e28") & ";" & Range("e29") & ";" & Range("e30") & ";"
.Subject = Range("b2")
.HTMLBody = Range("c2")
.Send 'or use .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