vba 转发电子邮件并附加附加文本而不会丢失原始邮件的格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10450771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Forward an email and append additional text without losing format of original message
提问by Ricky Valeroso
I'm trying to forward an email that I received and append an additional message on top of it. The following code I wrote kinda does this, but I lose all the formatting of the original message. Is there any way to maintain the format of the original message and yet able to append additional test to the email?
我正在尝试转发我收到的电子邮件并在其上附加一条附加消息。我写的以下代码有点这样做,但我丢失了原始消息的所有格式。有什么方法可以保持原始邮件的格式,并且能够在电子邮件中附加额外的测试?
MY CODE:
我的代码:
Sub xForward()
myMessage = "You recently requested access to the table. We are requiring all requests to complete a mandatory training session." & vbCr & vbCr & "Thank you, " & vbCr & "Ricky"
Dim itmOld As MailItem, itmNew As MailItem
Set itmOld = ActiveInspector.CurrentItem
Set itmNew = itmOld.Forward
itmNew.Body = myMessage & vbCr & vbCr & itmOld.Body
itmNew.Subject = "Access Request"
itmNew.Display
Set itmOld = Nothing
Set itmNew = Nothing
End Sub
If I don't update the body of itmNew, then I maintain the format of the original message. The moment I update itmNew.Body, then itmOld.Body is written in simple text and I lose all the formatting.
如果我不更新 itmNew 的正文,那么我将保持原始消息的格式。我更新 itmNew.Body 的那一刻,然后 itmOld.Body 是用简单的文本编写的,我丢失了所有格式。
回答by Tony Dallimore
I think JP's comment points you in the right direction but I assume your questions results from limited knowledge of HTML. This is not a complete tutorial on HTML but I hope it gets your started.
我认为 JP 的评论为您指明了正确的方向,但我认为您的问题源于对 HTML 的有限了解。这不是一个完整的 HTML 教程,但我希望它能帮助您入门。
If you use Debug.Print to output .HTMLBody to the immediate window you will see something like:
如果您使用 Debug.Print 将 .HTMLBody 输出到直接窗口,您将看到如下内容:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>
Lots of stuff here
</head> <body>
Lots of stuff here
</body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http ://www.w3.org/1999/xhtml"> <头>
这里有很多东西
</head> <body>
这里有很多东西
</正文> </html>
You will only get "<!DOCTYPE html ... " if the package that created the message supports the XML version of HTML. The minimum you should see is:
如果创建消息的包支持 HTML 的 XML 版本,您将只得到“<!DOCTYPE html ...”。您应该看到的最小值是:
<html><head> Lots of stuff here </head><body> Lots of stuff here </body></html>
<html><head> 这里有很多东西 </head><body> 这里有很多东西 </body></html>
If you place your extra message in front or at the end of this then you are breaking the rules of HTML. What happens will depend on how forgiving the receiver's email package is. To conform to the rules of HTML, you must place your extra message somewhere between "<body>" and "</body>".
如果您将额外的消息放在前面或后面,那么您就违反了 HTML 规则。会发生什么取决于收件人的电子邮件包的宽容程度。为了符合 HTML 的规则,您必须将额外的消息放在“<body>”和“</body>”之间。
If you look through a few messages, you will see how much they can vary. Some will be black text on white, some white text on black with every variation in between. Your message must be readable no matter what the message's authors have done. My suggestion is you create a one cell table at the top and you set the font and background colours. Try the following and then adapt it to your requirements:
如果您查看一些消息,您将看到它们的变化有多大。有些是白底黑字,有些是黑底白字,中间有各种变化。无论消息的作者做了什么,您的消息都必须是可读的。我的建议是您在顶部创建一个单元格表格并设置字体和背景颜色。尝试以下操作,然后根据您的要求进行调整:
Dim AddedMsg As String
Dim Pos As Long
' Create message to be inserted
' =============================
' Start a table with white background and blue text
AddedMsg = "<table border=0 width=""100%"" style=""Color: #0000FF"" bgColor=#FFFFFF>"
' Add row with single cell
AddedMsg = AddedMsg & "<tr><td><p>Cool stuff you must see!!</p></td></tr>"
' End table
AddedMsg = AddedMsg & "</table>"
' Code to add message once you have checked there is an HTML body
'================================================================
Pos = InStr(1, LCase(.HTMLBody), "<body")
If Pos = 0 Then
' This should be impossible.
Call MsgBox("<Body> element not found in HTML body", vbCritical)
' Code to handle impossible situation
End If
Pos = InStr(Pos, .HTMLBody, ">")
If Pos = 0 Then
' This should be impossible.
Call MsgBox("Terminating > for <Body> element not found in HTML body", vbCritical)
' Code to handle impossible situation
End If
'Insert your message
.HTMLBody = Mid(.HTMLBody, 1, Pos) & AddedMsg & Mid(.HTMLBody, Pos + 1)