PDA

View Full Version : Solved: I want to set column H to column V as the email body



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

Bob Phillips
12-02-2009, 07:50 AM
Untested



.HTMLBody = RangetoHTML(Cells(cell.Row, "H").Resize(,15)

clif
12-02-2009, 09:01 AM
It seems not work !

Bob Phillips
12-02-2009, 10:00 AM
More detail would help.

clif
12-03-2009, 05:34 AM
I put your code into body but not work, i have upload the excel file. Can you help me to test!

GTO
12-04-2009, 01:33 AM
Greetings Clif,

There's no attachment?

Mark

clif
12-04-2009, 05:19 AM
The attachment is in the first post ! "efg.xls"

GTO
12-07-2009, 09:13 PM
Greetings Clif,

Sorry I missed seeing the file before, and later forgot to take a look.

Anyways... while I admit the look a bit cursory, I did test and XLD's suggestion works as it's supposed to, albeit it needed a closing parenthesis.

To test, change:


With OutMail
.To = cell.Value
.Subject = Cells(cell.Row, "J").Value
.HTMLBody = RangetoHTML(Cells(cell.Row, "H").Resize(, 15))
'.HTMLBody = RangetoHTML(Selection)
'.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 _


'...etc

You may not be enamored with the results however, as Mr. De Bruin's example is more to take a "picture" if you will, of the wanted range.

To test that, you can un-comment:


'.HTMLBody = RangetoHTML(Selection)


...and select some cells. I chose C3:G6 and bordered them.

Hope that helps,

Mark

clif
12-08-2009, 05:04 AM
Thanks!

But how to present the words just like a email in outlook

Now the words are horizontal in outlook

How to present the word line by line

I creat this but not work

.HTMLBody = RangetoHTML(Cells(cell.Row, "H")) & vbNewLine & RangetoHTML(Cells(cell.Row, "I")) & vbNewLine _
& RangetoHTML(Cells(cell.Row, "J")) & vbNewLine & RangetoHTML(Cells(cell.Row, "K")) & vbNewLine _
& RangetoHTML(Cells(cell.Row, "L")) & vbNewLine & RangetoHTML(Cells(cell.Row, "M")) & vbNewLine _
& RangetoHTML(Cells(cell.Row, "N")) & vbNewLine & RangetoHTML(Cells(cell.Row, "O")) & vbNewLine _
& RangetoHTML(Cells(cell.Row, "P")) & vbNewLine & RangetoHTML(Cells(cell.Row, "Q")) & vbNewLine _
& RangetoHTML(Cells(cell.Row, "R")) & vbNewLine & RangetoHTML(Cells(cell.Row, "S")) & vbNewLine _
& RangetoHTML(Cells(cell.Row, "T")) & vbNewLine & RangetoHTML(Cells(cell.Row, "U")) & vbNewLine _
& RangetoHTML(Cells(cell.Row, "V")) & vbNewLine _

clif
12-17-2009, 03:59 AM
I want to present the content vertically in outlook. Anyone can help me ?

GTO
12-18-2009, 01:06 AM
But how to present the words just like a email in outlook

Now the words are horizontal in outlook

How to present the word line by line

Hi Clif,

I am not understanding. Your original code, or something very close to it, would seem to do just what you want. That is, using .Body and the linefeeds...

.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 _


...would return (from your attached example) something like:

Dear John
Re: Letter of accept
Please be noted that your application was successful.
congratulation !
X
0
Yours faithfully,
Alan
______________
Chan Siu Ming
Encl.: Consent Document


What is it that you want the message to be layed out like?

Mark

clif
12-18-2009, 05:14 AM
I want the following outcome:

Dear John
Re: Letter of accept

Please be noted that your application was successful.
congratulation !
X
0
Yours faithfully,
Alan
______________
Chan Siu Ming
Encl.: Consent Document



But i use the code and function RangetoHTML() it will present horizontally in outlook. (This may the excel cell range is horizontal). How can i solve it?
Can you provide the code to me? Thank you very much!

GTO
12-18-2009, 04:49 PM
Greetings Clif,

Okay, this should be the last try for me, as I can barely spell HTML, much less parse/markup to any great success...

As I mentioned, Ron DeBruin's function is to basically insert a picture if-you-will, of the range. I do not believe it lends itself to what you are trying to do, which I believe is to format the body text.

To do this, I believe you are on course using .HTMLBody, but to use that, you must put all the funny looking html tags in.

So here is my shot, rude/crude as it may be...

At the top of the module:


Enum eFontChoice
FBold = 1
FItalic = 2
FBldItal = 3
FUnderline = 4
End Enum


For .HTMLBody


.HTMLBody = HTML_SimpleMarkup(Cells(cell.Row, "H").Value, FBldItal) & _
"<BR>" & "<BR>" & _
HTML_SimpleMarkup(Cells(cell.Row, "J").Value, FUnderline) & _
"<BR>" & "<BR>" _
& Cells(cell.Row, "L").Value & "<BR>" & Cells(cell.Row, "M").Value _
& "<BR>" & Cells(cell.Row, "N").Value & "<BR>" & _
Cells(cell.Row, "O").Value & "<BR>" & Cells(cell.Row, "P").Value _
& "<BR>" & HTML_SimpleMarkup(Cells(cell.Row, "R").Value, FBldItal) _
& "<BR>" & "<BR>" & Cells(cell.Row, "S").Value & "<BR>" _
& Cells(cell.Row, "T").Value & "<BR>" & Cells(cell.Row, "V").Value


Add a Function below your current sub/function


Function HTML_SimpleMarkup(CellText As String, FOption As eFontChoice) As String

Select Case FOption
Case FBold
HTML_SimpleMarkup = "<STRONG>" & CellText & "</STRONG>"
Case FItalic
HTML_SimpleMarkup = "<EM>" & CellText & "</EM>"
Case FBldItal
HTML_SimpleMarkup = "<STRONG><EM>" & CellText & "</EM></STRONG>"
Case FUnderline
HTML_SimpleMarkup = "<U>" & CellText & "</U>"
End Select
End Function


Hope that helps,

Mark

GTO
12-18-2009, 04:55 PM
ACK! I meant to add a post script...

I was thinking that you might want to start a thread in the Outlook forum and reference/link to this.

clif
12-18-2009, 06:10 PM
Thank you very much! This is what i want!

GTO
12-19-2009, 03:28 AM
Glad to help Clif; sorry it wasn't 'prettier'/more efficient.

Have a great day :-)

Mark