[FONT=Fixedsys] Dim iRow As Long
Dim iColumn As Long[/FONT]
[FONT=Courier New][/FONT]
[FONT=Fixedsys] .HTMLBody = .HTMLBody _[/FONT]
[FONT=Fixedsys] & "< table border='1' bordercolor='#000000' cellpadding='4' cellspacing='2'>"
For iRow = 1 To 10
.HTMLBody = .HTMLBody & "< tr>"
For iColumn = 1 To 6
.HTMLBody = .HTMLBody & "< td>" & Cells(iRow, iColumn) & "< /td> "
Next iColumn
.HTMLBody = .HTMLBody & "< /tr>"
Next iRow
.HTMLBody = .HTMLBody & "< /table>"
[/FONT]
< table border='1' bordercolor='#000000' cellpadding='4' cellspacing='2'>
< tr>
< td>UPN< /td> < td>Surname< /td> < td>Forename< /td>
< td>DOB< /td> < td>Gender< /td> < td>Class< /td>
< /tr>
< tr>
< td>D916110408240< /td> < td>Aquinas< /td> < td>Thomas< /td> < td>08/01/1997< /td> < td>M< /td> < td>RA< /td>
< /tr>
< /table>
Sub Send_Range_Or_Whole_Worksheet_with_MailEnvelope()
Dim AWorksheet As Worksheet
Dim Sendrng As Range
Dim rng As Range
'Declare variables for ado connection and sql command
Dim oCon, strBuild, strSql
'These variables will store data from excel spreadsheet
Dim StrUser As String
StrUser = Environ("Username")
'Instantiate the connection object and specify connection properties
Set oCon = CreateObject("adodb.connection")
oCon.Provider = "Microsoft.ACE.OLEDB.12.0"
'Open the connection
'Use a UNC to a file on a server or the path & filename name
oCon.Open "c\\tempfolder"
strSql = "SELECT u373091.Client_Name, u373091.Product, u373091.Policy_Number, u373091.Net_Amount " _
& "FROM u373091 " _
& "WHERE (((u373091.Status) = 'cancelled')) " _
& "GROUP BY u373091.Client_Name, u373091.Product, u373091.Policy_Number, u373091.Net_Amount;"
On Error GoTo StopMacro
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Fill in the Worksheet/range you want to mail
'Note: if you use one cell it will send the whole worksheet
Set Sendrng = Worksheets("STATEMENT").Range("A1:B5")
'Remember the activesheet
Set AWorksheet = ActiveSheet
'Create the mail and send it
With Sendrng
' Select the worksheet with the range you want to send
.Parent.Select
'Remember the ActiveCell on that worksheet
Set rng = ActiveCell
'Select the range you want to mail
.Select
' Create the mail and send it
ActiveWorkbook.EnvelopeVisible = True
With .Parent.MailEnvelope
' Set the optional introduction field thats adds
' some header text to the email body.
.Introduction = "This is a test mail."
' In the "With .Item" part you can add more options
' See the tips on this Outlook example page.
' [URL]http://www.rondebruin.nl/mail/tips2.htm[/URL]
With .Item
.To = "[EMAIL="peter_z@mrexcel.com"]peter_z@mrexcel.com[/EMAIL]"
.Subject = "My subject"
.Send
End With
End With
'select the original ActiveCell
rng.Select
End With
'Activate the sheet that was active before you run the macro
AWorksheet.Select
StopMacro:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
ActiveWorkbook.EnvelopeVisible = False
End Sub
rst.Open strSQL, cnn
rst.MoveFirst
strHTML = "< table border=1>"
While Not (rst.EOF)
For Each fld In rst.Fields
strCells = strCells & "< td>" & fld.Value & "< /td>"
Next fld
strHTML = strHTML & "< tr>" & strCells & "< /tr>"
strCells = ""
rst.MoveNext
Wend
strHTML = strHTML & "< /table>"
'Set Body for mail
aEmail.Body = vbLf & strClientSQL & strPolicySQL _
& vbLf & strProductSQL _ 'Change Body Message
aEmail.Body = aEmail.Body 'Set attachment
aEmail.Attachments.Add StrOutput
'Send Mail
aEmail.Display