PDA

View Full Version : Combining HTML and text box entry into message body



lifeson
09-17-2008, 11:51 AM
I use the following code to generate a message which is a combination of HTML (so I can include a hyperlink) and the entry from a text box on a user form.
However even though there are carriage returns in the text from the text box when it is combine with the HTML body the returns aren't recognised and the text is all on one line

the eDetail string comes from a txtbox on the user form

Function sendMail(subj As String, eDetail As String)
Dim OutlookApp As Object
Dim MItem As Outlook.MailItem
Dim eMail As String, id As String, sentBy As String, msg As String, HTMLmsg As String
Dim hLink As String
Dim r As Long, i As Long
Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("users")

sentBy = Me.cboLogged.Value

'Format mail item
With Me.lvwUsers
r = .ListItems.Count
'loop through list
For i = 1 To r
Me.lblMsg.Caption = "Checking row: " & i & " of: " & r
If .ListItems.Item(i).Checked = True Then
id = .ListItems.Item(i)
eMail = eMail & "; " & ws.Columns(1).Find(id).Offset(, 1).Text
Else
'not checked
End If
Next i

End With

'Create Outlook object
Set OutlookApp = New Outlook.Application
'create mail item
Set MItem = OutlookApp.CreateItem(olMailItem)

'Link to error database on teamsite
hLink = "http://link to external site"

HTMLmsg = sentBy & "<br>" & _
"Has logged the following <b>" & sMajor & "</b> error on the database.<br><br>" & _
eDetail & "<br><br>" & _
"Click the link to go to the database.<br>" & _
"<A HREF=" & hLink & ">Link to Team Site</A>" & _
"<br><br><B>Thank you</B>"

If eMail = "" Then
MsgBox "No one has been selected!", vbOKOnly
Exit Function
Else
With MItem
.To = eMail
.Subject = subj
'.Body = msg
.HTMLBody = HTMLmsg
.Importance = 2 'high
If fName <> "" Then .Attachments.Add fName
.Display
'.Send
End With 'mail item

End If
End Function

How do I keep the returns in the text box in the HTML message?

Kenneth Hobs
09-17-2008, 12:44 PM
Not sure what you mean. If you have the MuliLine=True for the textbox and Alt+Enter is used, you will have a vbCrLf. You can use Replace() to replace TextBox1.Value with whatever you need like vbLf, vbCr, "<HR>", etc.