Outlook VBA losing formatting with replace function and .HTMLBody won't work

浪子不回头ぞ 提交于 2019-12-12 04:55:17

问题


I took the liberty of scouring the Outlook VBA stack overflow section of the site, and could not find anything related to this question. I have a template I created. When I open the template the formatting is already as I want it and the signature auto-populates at the end of the message. I have created a macro that prompts for the following information and then replaces my tags in the message with the information I entered in Input Boxes.

After using Google (a LOT) I tried using myItem.Body in my replace function. When I do this the macro works but I lose all template and signature formatting. Then I tried using myItem.HTMLBody in my replace function and it does not replace anything in the body and the signature disappears.

Code below... any guidance would be greatly appreciated. (Outlook 2010)

Sub PopulateTemplate()
Dim myItem As MailItem, oInspector As Inspector
Set oInspector = Application.ActiveWindow
Set myItem = oInspector.CurrentItem

'Provide the email address(es) and populate in the To field
strEmailAddr = InputBox("Enter email address(es), separated by semicolon(;)")
myItem.To = Replace(myItem.To, "<EmailAddr>", strEmailAddr)
myItem.Display

'Provide the project name and populate in subject and body of email
strProjName = InputBox("Enter the Project Name")
myItem.Subject = Replace(myItem.Subject, "<ProjectName>", strProjName)
myItem.Body = Replace(myItem.Body, "<ProjectName>", strProjName)
myItem.Display

'Provide the time of day for the greeting and populate in the body of email
strTimeofDay = InputBox("Enter Morning, Afternoon, or Evening")
myItem.Body = Replace(myItem.Body, "<TimeofDay>", strTimeofDay)
myItem.Display

'Provide the first name for the greeting and populate in body of email
strContactName = InputBox("Enter the First Name for the Greeting")
myItem.Body = Replace(myItem.Body, "<ContactName>", strContactName)
myItem.Display

'Resolve all recipients (Same as pressing the "Check Names" button)
Call myItem.Recipients.ResolveAll

'Free memory
Set strEmailAddr = Nothing
Set strProjName = Nothing
Set strTimeofDay = Nothing
Set strContactName = Nothing
End Sub

回答1:


Your HTML body would not have "<" and ">" text characters (as in "<ProjectName>"), they will be HTML encoded - &lt; and &gt;




回答2:


Outlook uses Word as an email editor. You can use the Word object model for making modifications in the message body, at least the WOM provides a convenient methods for replacing pieces of text. To get things working you need to use the WordEditor property of the Inspector class which returns an instance of the Document class from the Word object model. You can find all possible ways of working with item bodies described in the Chapter 17: Working with Item Bodies (including the sample code).



来源:https://stackoverflow.com/questions/29660647/outlook-vba-losing-formatting-with-replace-function-and-htmlbody-wont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!