0

I scoured the Outlook VBA section of the site, and could not find anything related.

I created a template. The formatting is as I want it and the signature auto-populates.

I created a macro that replaces tags in the message with information entered in Input Boxes.

I tried using myItem.Body in my replace function. I lose all template and signature formatting.

I tried using myItem.HTMLBody in my replace function. It does not replace anything in the body and the signature disappears.

Code (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
Community
  • 1
  • 1
Andy
  • 3
  • 4
  • Your code does not do anything with the HTMLBody property, only the plain text Body. What is your code that uses HTMLBody? – Dmitry Streblechenko Apr 15 '15 at 21:20
  • just replace any instance of myItem.Body with myItem.HTMLBody. I tried that method as well and 1)nothing in the body of the text got replaced and 2)the signature disappeared from the message. – Andy Apr 15 '15 at 21:24

2 Answers2

1

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

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Simple Solution! Just changed them to square brackets and it fixed everything. Sometimes we just need a fresh set of eyes. Thanks again. – Andy Apr 16 '15 at 12:56
0

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).

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Although Dmitry's solution worked, I did take the time to bookmark your hyperlink and read the passage last night. It was definitely enlightening! Thanks for the reference materials. – Andy Apr 16 '15 at 12:57