1

Example: An interview confirmation email in Outlook.

Dear [A],
Your interview with [B] is confirmed for [C]. The address is [D].
Thank you,
John Doe

A - First Name
B - Company Name
C - Date & Time
D - Physical Address

I've seen people use Excel for this, but I want to be able to just enter A, B, C and D values somewhere, click send, and that's it.

All the code I've come across can achieve variable factors, but hardly any had codes for the body of the email, and if they did, NONE of them incorporated .oft files.

CLSA
  • 89
  • 1
  • 10
B.McInnis
  • 11
  • 1
  • 2

1 Answers1

1

Your question implies you were expecting to find some code that exactly matched your requirement. Perhaps this is unfair criticism but many people do seem to expect their exact requirement to already exist or for someone to code it for them.

If I have a requirement that needs expertise with functionality that is new to me, I research each area separately and then experiment before trying to use my new knowledge to meet my requirement.

The first knowledge needed for your requirement is to create an email template file. I assume you already know how to do this but, if not, there are many websites that explain how to do it. I saved my template with the name “StdMsg.oft”. You will need to amend my macro to use your name for your template.

You can save an email template file anywhere but it is probably best to use the default location so all your templates are together. When you want to open the template, your macro will need to know where it is. The default location, for Windows 10, is “C:\Users\xxxxx\AppData\Roaming\Microsoft\Templates” where “xxxxx” is your user name. You can type this string out but, if you wish to share this macro with colleagues, you may wish to ask the system for the current value of “xxxxx”. I easily found a site which gave me the default location but finding a site which told me how to get the value of “xxxxx” was more difficult. I found plenty of sites that told me how to get the value of “xxxxx” but most were wrong or assumed knowledge that the reader might not have. I keep a folder named “Resources” with subfolders such as Html, Css, VBAExcel, VBAOutlook and VBAGeneral. These subfolders contain text files recording useful snippets that I am unlikely to remember. I suggest you create a similar folder and record how I obtained the value of “xxxxx”.

Next you need to open the email template file. I found plenty of sites that showed how to do so from Excel but I did not find a single site that showed how to do so correctly from Outlook. From Excel, you must create an Outlook application object. From Outlook, you are already within an Outlook application object so you do not need to create another. In fact, you cannot create a new Outlook application object because Outlook only allows one version of itself to be running. If you try to create a new Outlook application object, you are given a reference to the copy that is already running. Again you may wish to record this information in a resource folder.

I have used InputBox to obtain the four values from the user. I would use a form if I was writing this macro for myself or a client. It is difficult to show how to create a form which would be wasted effort if you already know.

Once the email template is open, you have access to all its properties. There are many answers on this site, and elsewhere, that show how to amend the html or the text body of an email. I assume you want to send html emails so I have amended the html body.

My macro displays the edited email but you could use Send instead of Display if you don’t want to check the email before it is sent.

Sub SendStdMsgOft()

  Dim Address As String
  Dim CompanyName As String
  Dim DateAndTime As String
  Dim FirstName As String
  Dim NewEmail As MailItem
  Dim PathFileName As String

  PathFileName = Environ("AppData") & "\Microsoft\Templates\StdMsg.oft"

  Set NewEmail = CreateItemFromTemplate(PathFileName)

  FirstName = InputBox("First name")
  CompanyName = InputBox("Company name")
  DateAndTime = InputBox("Date And Time")
  Address = InputBox("Address")

  With NewEmail
    .HtmlBody = Replace(.HtmlBody, "[A]", FirstName)
    .HtmlBody = Replace(.HtmlBody, "[B]", CompanyName)
    .HtmlBody = Replace(.HtmlBody, "[C]", DateAndTime)
    .HtmlBody = Replace(.HtmlBody, "[D]", Address)
    .Display
  End With

End Sub
Tony Dallimore
  • 12,335
  • 7
  • 32
  • 61