clif
12-02-2009, 07:43 AM
If each row in excel for one email, I want to set column H to column V as the email body, how can i set the range and use the function of RangetoHTML
Here is the code
Sub email()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range, FileCell As Range, rng As Range
Dim sh As Worksheet
Set sh = Sheets("Sheet1")
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
For Each cell In Columns("C").Cells.SpecialCells(xlCellTypeConstants)
Set rng = sh.Cells(cell.Row, 1).Range("F1:G1")
If cell.Value Like "?*@?*.?*" And _
LCase(Cells(cell.Row, "D").Value) = "y" Then
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = cell.Value
.Subject = Cells(cell.Row, "J").Value
.Body = Cells(cell.Row, "H").Value & vbNewLine & Cells(cell.Row, "I").Value & vbNewLine _
& Cells(cell.Row, "J").Value & vbNewLine & Cells(cell.Row, "K").Value & vbNewLine _
& Cells(cell.Row, "L").Value & vbNewLine & Cells(cell.Row, "M").Value & vbNewLine _
& Cells(cell.Row, "N").Value & vbNewLine & Cells(cell.Row, "O").Value & vbNewLine _
& Cells(cell.Row, "P").Value & vbNewLine & Cells(cell.Row, "Q").Value & vbNewLine _
& Cells(cell.Row, "R").Value & vbNewLine & Cells(cell.Row, "S").Value & vbNewLine _
& Cells(cell.Row, "T").Value & vbNewLine & Cells(cell.Row, "U").Value & vbNewLine _
& Cells(cell.Row, "V").Value & vbNewLine & Cells(cell.Row, "W").Value & vbNewLine _
For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
If Trim(FileCell) <> "" Then
If Dir(FileCell.Value) <> "" Then
.Attachments.Add FileCell.Value
End If
End If
Next FileCell
.Display
.Send
End With
End If
Next cell
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-2007
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
Here is the code
Sub email()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range, FileCell As Range, rng As Range
Dim sh As Worksheet
Set sh = Sheets("Sheet1")
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
For Each cell In Columns("C").Cells.SpecialCells(xlCellTypeConstants)
Set rng = sh.Cells(cell.Row, 1).Range("F1:G1")
If cell.Value Like "?*@?*.?*" And _
LCase(Cells(cell.Row, "D").Value) = "y" Then
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = cell.Value
.Subject = Cells(cell.Row, "J").Value
.Body = Cells(cell.Row, "H").Value & vbNewLine & Cells(cell.Row, "I").Value & vbNewLine _
& Cells(cell.Row, "J").Value & vbNewLine & Cells(cell.Row, "K").Value & vbNewLine _
& Cells(cell.Row, "L").Value & vbNewLine & Cells(cell.Row, "M").Value & vbNewLine _
& Cells(cell.Row, "N").Value & vbNewLine & Cells(cell.Row, "O").Value & vbNewLine _
& Cells(cell.Row, "P").Value & vbNewLine & Cells(cell.Row, "Q").Value & vbNewLine _
& Cells(cell.Row, "R").Value & vbNewLine & Cells(cell.Row, "S").Value & vbNewLine _
& Cells(cell.Row, "T").Value & vbNewLine & Cells(cell.Row, "U").Value & vbNewLine _
& Cells(cell.Row, "V").Value & vbNewLine & Cells(cell.Row, "W").Value & vbNewLine _
For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
If Trim(FileCell) <> "" Then
If Dir(FileCell.Value) <> "" Then
.Attachments.Add FileCell.Value
End If
End If
Next FileCell
.Display
.Send
End With
End If
Next cell
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-2007
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