David Cunha
David Cunha

Reputation: 1

VBA HTML Tags in email. Are my tags properly closed & nested?

I received an odd error message from a website named Unspan stating that my HTML Tags in my test email were not properly closed and nested.

I am seeking a second pair of eyes to review the code written below as I do not visually see where my error lies.

Attached is a screen shot of the error message as well as the code written itself. If someone could kindly review my work and point out what I did incorrect and what I need to change, that would be much appreciated thank you.

HTML error message

enter image description here

Error message received

The HTML body is not composed of properly closed and nested tags

The HTML body is not composed of standard and supported HTML elements

Outlook email message printed

Dim OutApp As Object
Dim OutMail As Object
Dim Ws As Worksheet
Dim strbody As String
Dim row_count As Integer


Greeting = Ws.Range("I24")
EMD = Ws.Range("B7")
DPeriod = Ws.Range("B8")
CashEscrow = Ws.Range("A9")
CashDays = Ws.Range("B9")
CreativeEscrow = Ws.Range("A10")
CreativeDays = Ws.Range("B10")
Body = Ws.Range("B19")
CC = Ws.Range("F2")
BCC = Ws.Range("H2")

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)


strbody = "<BODY style = font-size:14pt;font-family:Tahoma>" & _
Greeting & " " & Ws.Cells(i, 1).Text & "," & "<br>" & Body & " " & Ws.Cells(i, 4).Text & "." & _
"<br>" & "<br>" & "Non-Refundable EMD" & " " & "$" & EMD & "<br>" & "DD Period" & " " & DPeriod & "<br>" & CashEscrow & " " & _
CashDays & "<br>" & CreativeEscrow & " " & CreativeDays & "<br>" & "<br>" & "Thank You" & "<br>" & "Regards," & "<br>" & Ws.Range("K2") & "<br>" & Ws.Range("L2") & "<br>" & Ws.Range("K3") & "<br>" & Ws.Range("L3") & "<br>"


FileName = Ws.Cells(i, 6).Text
 
On Error Resume Next
    With OutMail
        .To = Ws.Cells(i, 2).Text
        .CC = CC
        .BCC = BCC
        .Subject = "Interested In Purchasing" & " " & Ws.Cells(i, 4).Text & ":" & " " & "Please Review"
        .HTMLBody = strbody & _
        "<img src='C:\Users\david\OneDrive\Desktop\Real Estate\Whoop&Discord\Credibility Evergreen Servicing\WhyRealtorsshouldUseCreativeFinance.jpg' width='90%' height='90%'>" & _
        .HTMLBody
      

Checked the other forums, Youtube and google. Tried to find the error in my code manually

Below is the Print Statement from the Immediate VBA Window

<BODY style = font-size:16pt;font-family:Tahoma>
Good Afternoon Keianna,<br>Hope all is well on this Thursday. Quickly following up on our offer sent last week. My partner Laura and I are interested in purchasing one of your listed properties. We have constructed 2 main offers for sellers consideration. Attached is our Letter of Intent to purchase along with our credibility. Both proposals are subject to final underwriting. The necessary paperwork will be written up on our end, and attached as an addendum to your state approved PSA when the seller chooses options 2 or 3. Should yourself, or the seller have any questions my contact info is below. We look forward to acquiring, 828 Royal Red Ct.<br><br>Non-Refundable EMD $4000<br>DD Period 7 days Excluding Sundays<br>Offer One: Close In 30 Days<br>Offer Two: Close On or Before 21 Days<br><br>Thank You<br>Regards,<br>David Elliot<br>Cell 914-426-2428<br>Office Contact<br>  470-339-6093<br>
</body>
<img src='C:\Users\david\OneDrive\Desktop\Real Estate\Whoop&Discord\
    Credibility Evergreen Servicing\WhyRealtorsshouldUseCreativeFinance.jpg' width='90%' height='90%'>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
    <HTML>
    <HEAD>
    <META NAME="Generator" CONTENT="MS Exchange Server version 16.0.17928.20114">
    <TITLE></TITLE>
    </HEAD>
    <BODY>
    <!-- Converted from text/plain format -->
    
    </BODY>
    </HTML>

Upvotes: 0

Views: 85

Answers (3)

FunThomas
FunThomas

Reputation: 29146

If you look to the dumped output of .HTMLBody, you can easily see that the HTML is invalid.

The main reason is that you concatenate the HTML you generate and the already existing default code of .HTMLBody. Looking at the default code on my computer (yours may differ), it shows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->

</BODY>
</HTML>

Which is a complete (but of course empty) HTML page.

Now in your code, you put your code (strbody) first, plus an IMG tag, and then adds that default code - which results in invalid HTML code as the <HTML> must the the outermost tag.

(An additional problem comes from the fact that your own code opens a tag but doesn't close it, which results in unmatched tags.)

Const ImgTag As String = "<img src='C:\Users\david\OneDrive\Desktop\Real Estate\Whoop&Discord\Credibility Evergreen Servicing\WhyRealtorsshouldUseCreativeFinance.jpg' width='90%' height='90%'>"
Const BodyTag As String = "<BODY style = font-size:14pt;font-family:Tahoma>"

Dim content As String
content = "Greeting & " " & ws.Cells(i, 1).Text & "," & "<br>" & Body & " " 
(...)

You have 2 possibilities: Either you write the complete HTML code by yourself:

.HTMLBody = "<HTML>" & BodyTag & content & ImgTag & "</BODY> & </HTML>

Or you inject your content into the default code:

.HTMLBody = .HTMLBody.Replace("<BODY>", BodyTag & Content & ImgTag)

Upvotes: 0

xShen
xShen

Reputation: 572

I'm not familiar with "unspam email website".

The only official place to validate html is https://validator.w3.org/#validate_by_input

according to the print statements your HTML is wrong, HTML structure is all about opening tags and closing tag. but the answer is out of the scope in this vba excel tag. So if you want to learn how to write an HTML there is a lot of places to lern on the net

Upvotes: 0

xShen
xShen

Reputation: 572

Although you declared WS but you never initialize it you have to do it like

Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1") ' or whatever the sheet name

you have to give the i initial value of something like 1

Dim i As Long: i = 1

At the end of strbody you have to add a tag to close the tags something like

strbody = <BODY style = font-size:14pt;font-family......

strbody = strbody & "</body>"

Hopefully this will do the trick

Upvotes: 0

Related Questions