Sending html email from VBA email

I wrote an email program for my organization that handles some very specialized things very well that I can use for Outlook or Gmail. Now the manager would like to send a random email to our small client base, but I want the email body to look professional and not send it as an application. I combined the html document that is present in all browsers and was checked. My problem is that I cannot figure out how to specify the body of the message in the html document. Here is the main code.

Everything is configured here:

Do While mailRs.EOF = False 'Me.AttachDoc = "C:\EmailFolder\CouponForm.pdf" emTo = mailRs.Fields("EmailAddr").Value emFrom = " SportsParkInfo@skokieparks.org " emSubject = Me.Subject emtextBody = Me.TextMessage 

Here is a call to send an email

 Call SendAMessage(emFrom, mailRs.Fields("EmailAddr").Value, _ emSubject, emtextBody, emAttach) 

(I received a code to send email from the Internet, and it works great through our mail server.)

In the above, before calling @ emtextBody = Me.TextMessage , where I need to replace Me.TextMessage address / body of the html document. And the message field is a text field in the form of ACCESS. I can not find control over ACCESS that accepts html. I can not use the path to the html document because this is throwing an error. Is there any way around this.

If you need more information, I will be happy to provide it.

Thank you for your time.

jpl

+6
source share
2 answers

Use something like the code below. I have included elements for nesting as well as html formatting, but pretty much everything you can write in html can also be done in vba.

 Sub SharePerformance() Dim OutApp As Object Dim OutMail As Object Dim rng As Range Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.createitem(0) '& "<a href=""\\server\folder"">\\server\folder</a>" & msg1 = "Team,<br><br><b><DL>" & Range("b5").Value & "</b><br><ul><b><u>" & Range("b6").Value & "</b></u>" msg1 = msg1 & "<DT><a HREF=C:\USER\Desktop\File1.xlsb>" msg1 = msg1 & Range("b7").Value & "</a><br>" msg1 = msg1 & "<b><u>" & Range("b9").Value & "</b></u></DL><br><br>" msg1 = msg1 & "<p><img src=file://" & "C:\temp\Chart1.png" & "></p>" & "<br>" On Error Resume Next ' Change the mail address and subject in the macro before you run it. With OutMail .To = Range("B1").Value .cc = "" .BCC = "" .Subject = Range("B3").Value .HTMLBody = msg1 '.Attachments.Add ActiveWorkbook.FullName '.Attachments.Add ("C:\temp\Chart1.png") '.Attachments.Add ("C:\temp\Chart2.png") .display End With SendKeys "^{ENTER}" On Error GoTo 0 Set OutMail = Nothing Set OutApp = Nothing End Sub 
+7
source

I can’t tell what code is inside this SendAMessage function that you use, but all the VBA examples I worked with seem to work the same way with the CDO.Message object, as in this article in the KB286431 knowledge base . At some point in SendAMessage line appears that sets the value of the .TextBody message object to the .TextBody parameter that you pass.

One solution would be to copy the SendAMessage function to a new SendAMessageHTML function and replace the line where they set someMessage.TextBody = emtextBody , so you set someMessage.HTMLBody = emtextBody

Assuming your text text has text in the lines "<html><head><body></body></html>" , you can modify an existing function to perform a naive check like this:

 if Left(UCase(emtextBody),6) = "<HTML>" then someMessage.HTMLBody = emtextBody else someMessage.TextBody = emtextBody end if 
0
source

Source: https://habr.com/ru/post/984632/


All Articles