excel - VBA How do I place HTML in an email that sends through an Access module -
i have vba code has been given me sends email attachment through ms access:
sub email_send() dim strto string dim strcc string dim strfrom string dim strsubject string dim strmessage string dim intnrattch integer dim strattachments string dim strattachments2 string dim contact_name string dim email_address string dim cc_address string dim column1 adodb.recordset dim cnndb adodb.connection dim area string dim connection string dim basepath string dim region string dim column2 string dim upc string dim name string dim firstname string dim title string dim surname string dim bold string dim string basepath = "my path" set cnndb = new adodb.connection cnndb .provider = "microsoft.jet.oledb.4.0" .connectionstring = "my connection string" .open end set rstrst = new adodb.recordset rstrst .source = "select [column1], [column2], [column3]" & _ "from table1" rstrst.open , cnndb rstrst.movefirst while not rstrst .eof column1 = rstrst.fields("column1") column2 = rstrst.fields("column2") column3_address = rstrst.fields("column3") dim greeting string if time >= #12:00:00 pm# greeting = "afternoon," else greeting = "morning," end if dim currentmonth string currentmonth = monthname(month(date)) strmessage = "good" & greeting & chr(13) strmessage = strmessage & chr(13) strmessage = strmessage & "...text..." & chr(13) strmessage = strmessage & chr(13) strmessage = strmessage & "...text..." & chr(13) strmessage = strmessage & "" & chr(13) strmessage = strmessage & "...text..." & chr(13) strmessage = strmessage & "" & chr(13) strmessage = strmessage & "...text..." & chr(13) strmessage = strmessage & "" & chr(13) strmessage = strmessage & "...text..." & chr(13) strmessage = strmessage & "...text..." & chr(13) strmessage = strmessage & chr(13) strmessage = strmessage & "...text..." & chr(13) strmessage = strmessage & "...text..." & chr(13) strto = email_address 'strcc = cc_address strsubject = "information: ...text..." & column2 & "...text..." intnrattch = 1 strattachments = basepath & column1 & "file.xls" call sendmessageto(strto, strsubject, strmessage, intnrattch, strattachments) rstrst.movenext loop msgbox "sent" nowexit: end sub public function sendmessageto(strto string, strsubject string, strmessage string, intnrattch integer, strattachments string) boolean const nr = 9 dim myoutlook object dim mymessage object dim objnamespace dim strfiles(nr) string dim strpromt string dim integer, intlen integer dim intstart, intpos integer on error goto error_handler sendmessageto = false set myoutlook = createobject("outlook.application") set mymessage = myoutlook.createitem(0) if strto = "" strpromt = "you need specify e-mail address wich want send e-mail" msgbox strpromt, vbinformation, "send message to... ?" exit function end if if intnrattch > nr + 1 strpromt = "you can add " & nr + 1 & " attachments. if want add more need change array size" msgbox strpromt, vbcritical, "number of attachments" end if intstart = 1 intlen = 0 if strattachments <> "" = 0 intnrattch - 1 if < intnrattch - 1 intlen = instr(intstart, strattachments, ";") - intstart + 1 strfiles(i) = trim(mid(strattachments, intstart, intlen - 1)) intstart = intstart + intlen else strfiles(i) = trim(mid(strattachments, intstart, len(strattachments) - intstart + 1)) end if next end if intpos = len(strmessage) + 1 mymessage .to = strto .subject = strsubject .body = strmessage strattachments = "1" if strattachments <> "" = 0 intnrattch - 1 .attachments.add strfiles(i), 1, intpos next end if .send end set mymessage = nothing set myoutlook = nothing sendmessageto = true error_handler_exit: exit function error_handler: msgbox err.number & " : " & err.description, vbcritical, error resume error_handler_exit end function
what want use html format strmessage = "...text..."
example put in bold.
i have tried doing following:
set mymessage = myoutlook.createitem(0) mymessage .htmlbody = .htmlbody & "<br><b>weekly repport:</b><br>" _ & "<img src='cid:dashboardfile.jpg'" & "width='814' height='33'><br>" _ & "<br>best regards,<br>ed</font></span>" end
i have looked @ various websites, including: http://vba-useful.blogspot.co.uk/2014/01/send-html-email-with-embedded-images.html cannot work.
how can this?
first of all, don't mix .body , .htmlbody. pick one. want formatting & pic, .htmlbody need.
second: don't mix upper case , lower case html tags. use lower.
third: watch out invalid html, closing font , span tag have never been opened. use <br />
instead of <br>
(outdated).
fourth: set htmlbody entirely, not append it.
i don't know whether img display that's step 2 anyway. being said, try this:
mymessage.htmlbody = "<p class=msonormal>" & strmessage & "<br /><b>weekly report:</b><br />" _ & "<img src='cid:dashboardfile.jpg' width='814' height='33' /><br />" _ & "<br />best regards,<br />ed</p>"
edit: if wish keep line breaks in strmessage, replace chr(13)
<br />
first.
Comments
Post a Comment