Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XADM: A Replied or Forwarded CDO.Message Appears Rearranged on the Second Iteration of the Reply


View products that this article applies to.

This article was previously published under Q318944

↑ Back to the top


Symptoms

If you (as a developer) modify the HTMLBody property of a reply to a CDO.Message, the message may appear rearranged on the second iteration of the reply.

↑ Back to the top


Cause

This issue can occur if you did not modify the HTMLBody property of the reply to the CDO.Message correctly. When you modify the HTMLBody property of a reply to a CDO.Message, you must make sure that you create properly formatted Hypertext Markup Language (HTML) and insert it correctly into the body of the message.

Although the message appears rearranged, the message is actually still formatted just as you created it.

NOTE: The following code is simply an example to reproduce this issue. However, the situation that this example describes is possible, and has been encountered with third-party developers. This article describes what occurs in this situation and provides a method that you can use to correct your code.

For example, consider the following Microsoft Visual Basic code:
Public Sub Main()
Dim MsgName
Dim szPrefix
Dim Original As CDO.Message
Dim Reply As CDO.Message
Dim strReplyText1 As String
Dim strReplyText2 As String

strReplyText1 = "<div style=""{font-Weight:700;}"">This is REPLY HTML 1</div>"
strReplyText2 = "<div style=""{font-Weight:700;}"">This is REPLY HTML 2</div>"

MsgName = "HTMLMessage.EML"

szPrefix = ""
Set Original = New CDO.Message
Original.DataSource.Open "file://./backofficestorage/cpr-ex-lc.dom/mbx/charliea/inbox/" & szPrefix & MsgName
Set Reply = Original.Reply
Reply.HTMLBody = strReplyText1 + Reply.HTMLBody
Reply.From = "Charlie Andersen<charliea@cpr-ex-lc.dom>"
Reply.Send
Set Reply = Nothing
Set Original = Nothing

szPrefix = "RE%3A "
Set Original = New CDO.Message
Original.DataSource.Open "file://./backofficestorage/cpr-ex-lc.dom/mbx/charliea/inbox/" & szPrefix & MsgName
Set Reply = Original.Reply
Reply.HTMLBody = strReplyText2 + Reply.HTMLBody
Reply.From = "Charlie Andersen<charliea@cpr-ex-lc.dom>"
Reply.Send
Set Reply = Nothing
Set Original = Nothing

End Sub
					

Synopsis

During the INITIAL reply, Collaboration Data Objects (CDO) parses the following original message:
Received: from SMTPStorm ([157.57.146.15]) by tigger-530.cpr-ex-lc.dom with Microsoft SMTPSVC(5.0.2195.4453);
	 Thu, 14 Feb 2002 09:36:19 -0600
From: Charlie Andersen<charliea@cpr-ex-lc.dom>
To: Charlie Andersen<charliea@cpr-ex-lc.dom>
Subject: HTMLMessage
MIME-Version: 1.0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<html><body>Type message Here</body></html>
					
CDO reformats the original message with the correct reply headers in the body of the message as follows:
<html><body>
<BR>-----Original Message-----<BR>
<B>From:</B>	&quot;Charlie Andersen&quot; <charliea@cpr-ex-lc.dom>
<BR>
<B>Sent:</B>	14 February, 2002 09:36
<BR>
<B>To:</B>	&quot;Charlie Andersen&quot; <charliea@cpr-ex-lc.dom>
<BR>
<B>Subject:</B>	HTMLMessage
<BR>
<BR>
Type message Here</body></html>
					
At this point, the issue begins to compound. When you use the following syntax, you inadvertently create malformed HTML by placing HTML tags and text outside the <BODY> tags of the HTML stream:
Reply.HTMLBody = strReplyText1 + Reply.HTMLBody
					
You create a message with a body that is similar to the following:
<div>This is REPLY HTML 1</div>
<html><body>
<BR>-----Original Message-----<BR>
<B>From:</B>	&quot;Charlie Andersen&quot; <charliea@cpr-ex-lc.dom>
<BR>
<B>Sent:</B>	14 February, 2002 09:36
<BR>
<B>To:</B>	&quot;Charlie Andersen&quot; <charliea@cpr-ex-lc.dom>
<BR>
<B>Subject:</B>	HTMLMessage
<BR>
<BR>
Type message Here</body></html>
					
This in itself renders correctly in HTML view (in Microsoft Internet Explorer) because there are no items that should be viewed above the reply or forward header information. Internet Explorer is a very forgiving browser for malformed HTML. All of the Microsoft Outlook and Microsoft Outlook Express windows that display HTML use some form of the Internet Explorer HTML parsing and viewing mechanism.

When a user replies to the second message, the issues that are created by your (the third-party developer) code are made evident; this code opens the reply to the first message, and then attempts to reply to that message with another message.

CDO correctly adds all of the correct reply or forwarding information to the HTMLBody property and CDO correctly keeps all of that information inside the <BODY> tags of the message body.

CDO does not place the message text from the first reply in the <BODY> tag of the new message body because that message text was not found in the <BODY> tag to begin with (because of the programming error in the first reply). Therefore, when CDO commits the reply, the message is similar to the following:
<div>This is REPLY TEXT 1</div><html><body><BR>
-----Original Message-----<BR>
<B>From:</B>	&quot;Charlie Andersen&quot; <charliea@cpr-ex-lc.dom>
<BR>
<B>Sent:</B>	22 February, 2002 14:43
<BR>
<B>To:</B>	&quot;Charlie Andersen&quot; <charliea@cpr-ex-lc.dom>
<BR>
<B>Subject:</B>	RE: HTMLMessage
<BR>
<BR>
<BR>
-----Original Message-----<BR>
<B>From:</B>	&quot;Charlie Andersen&quot; <charliea@cpr-ex-lc.dom>
<BR>
<B>Sent:</B>	14 February, 2002 09:36
<BR>
<B>To:</B>	&quot;Charlie Andersen&quot; <charliea@cpr-ex-lc.dom>
<BR>
<B>Subject:</B>	HTMLMessage
<BR>
<BR>
Type message Here</body></html>
					
When you (the developer) run the following code, "<div>This is REPLY HTML 2</div>" (without the quotation marks) is inserted immediately preceding the text from the first reply:
Reply.HTMLBody = strReplyText2 + Reply.HTMLBody
					
Therefore, the message is displayed as follows:
<div>This is REPLY HTML 2</div>
<div>This is REPLY TEXT 1</div><html><body><BR>
-----Original Message-----<BR>
<B>From:</B>	&quot;Charlie Andersen&quot; =
<charliea@cpr-ex-lc.dom>
<BR>
<B>Sent:</B>	22 February, 2002 14:43
<BR>
<B>To:</B>	&quot;Charlie Andersen&quot; <charliea@cpr-ex-lc.dom>
<BR>
<B>Subject:</B>	RE: HTMLMessage
<BR>
<BR>
<BR>
-----Original Message-----<BR>
<B>From:</B>	&quot;Charlie Andersen&quot; <charliea@cpr-ex-lc.dom>
<BR>
<B>Sent:</B>	14 February, 2002 09:36
<BR>
<B>To:</B>	&quot;Charlie Andersen&quot; <charliea@cpr-ex-lc.dom>
<BR>
<B>Subject:</B>	HTMLMessage
<BR>
<BR>
Type message Here</body></html>
					
This renders the two lines of successive reply before any of the reply or forwarding information, and is nothing like what you intended.

↑ Back to the top


Resolution

To resolve this issue in your code, review the following code, which demonstrates how to correctly insert new HTML and text into an HTML-formatted message by using CDO:
 
' Function to split the BODY of the HTML into two parts, immediately after the end of the BODY tag.
' That way, the new message text is prepended ABOVE the prior message, but AFTER the body tag is complete.
'
Public Sub SplitBody(strHTML As String, strFirstPart As String, strLastPart As String)
    Dim lngPtr As Long
    Dim lngBodyBegin As Long
    Dim lngBodyEnd As Long
    Dim lngI As Long
    Dim lngLength As Long
    ' find <BODY in stream
    lngBodyBegin = InStr(1, UCase$(strHTML), "<BODY", vbTextCompare)
    ' did we find one?
    If lngBodyBegin > 0 Then
        ' Find the <BODY terminator
        lngBodyEnd = InStr(lngBodyBegin, strHTML, ">", vbTextCompare)
        ' did we find one?
        If lngBodyEnd > 0 Then
            strFirstPart = Left$(strHTML, lngBodyEnd)
            strLastPart = Mid$(strHTML, lngBodyEnd + 1)
        End If
    End If
End Sub

' A function that was written to provide a resolution to this issue.
' Insert a string of text after the body tag.
'
Sub InjectIntoBody(Msg As CDO.Message, HtmlText As String)
    Dim strHeader As String
    Dim strTrailer As String
    SplitBody Msg.HTMLBody, strHeader, strTrailer
    Msg.HTMLBody = strHeader + HtmlText + strTrailer
End Sub

Public Sub Main()
Dim MsgName
Dim szPrefix
Dim Original As CDO.Message
Dim Reply As CDO.Message
Dim strReplyText1 As String
Dim strReplyText2 As String

strReplyText1 = "<div style=""{font-Weight:700;}"">This is REPLY HTML 1</div>"
strReplyText2 = "<div style=""{font-Weight:700;}"">This is REPLY HTML 2</div>"

MsgName = "HTMLMessage.EML"

szPrefix = ""
Set Original = New CDO.Message
Original.DataSource.Open "file://./backofficestorage/cpr-ex-lc.dom/mbx/charliea/inbox/" & szPrefix & MsgName
Set Reply = Original.Reply
InjectIntoBody Reply, strReplyText1
Reply.From = "Charlie Andersen<charliea@cpr-ex-lc.dom>"
Reply.Send
Set Reply = Nothing
Set Original = Nothing

szPrefix = "RE%3A "
Set Original = New CDO.Message
Original.DataSource.Open "file://./backofficestorage/cpr-ex-lc.dom/mbx/charliea/inbox/" & szPrefix & MsgName
Set Reply = Original.Reply
InjectIntoBody Reply, strReplyText2
Reply.From = "Charlie Andersen<charliea@cpr-ex-lc.dom>"
Reply.Send
Set Reply = Nothing
Set Original = Nothing

End Sub 
					
In this example, CDO parses the HTML Message body to determine where the actual HTML body begins and inserts the new message text and HTML at that point.

↑ Back to the top


Keywords: KB318944, kbprb, kbmsg, kbarttypeinf

↑ Back to the top

Article Info
Article ID : 318944
Revision : 5
Created on : 10/26/2006
Published on : 10/26/2006
Exists online : False
Views : 247