简体   繁体   English

如何使用 MS Outlook 模板 (.oft) 发送电子邮件,并结合插入电子邮件正文的变量值(通过 Excel 或其他方式)

[英]How to send email using MS Outlook Template (.oft), combined with variable values inserted into body of email (through Excel or otherwise)

Example: An interview confirmation email in Outlook.示例:Outlook 中的面试确认电子邮件。

Dear [A] ,亲爱的[A]
Your interview with [B] is confirmed for [C] .您对[B]采访已确认用于[C] The address is [D] .地址是[D]
Thank you,谢谢,
John Doe约翰·多伊

A - First Name A - 名字
B - Company Name B - 公司名称
C - Date & Time C - 日期和时间
D - Physical Address D - 物理地址

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.我见过人们为此使用 Excel,但我希望能够在某处输入ABCD值,单击发送,就是这样。

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.我遇到的所有代码都可以实现可变因素,但几乎没有电子邮件正文的代码,如果有,它们都没有包含 .oft 文件。

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”.我使用名称“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.对于 Windows 10,默认位置是“C:\\Users\\xxxxx\\AppData\\Roaming\\Microsoft\\Templates”,其中“xxxxx”是您的用户名。 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”.您可以输入此字符串,但如果您希望与同事共享此宏,您可能希望向系统询问“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.我很容易找到一个给我默认位置的网站,但找到一个告诉我如何获取“xxxxx”值的网站比较困难。 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.我发现很多网站都告诉我如何获取“xxxxx”的值,但大多数都是错误的,或者是读者可能不知道的假设知识。 I keep a folder named “Resources” with subfolders such as Html, Css, VBAExcel, VBAOutlook and VBAGeneral.我保留一个名为“Resources”的文件夹,其中包含 Html、Css、VBAExcel、VBAOutlook 和 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”.我建议你创建一个类似的文件夹并记录我如何获得“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.我发现很多网站都展示了如何从 Excel 执行此操作,但我没有找到一个显示如何从 Outlook 正确执行此操作的站点。 From Excel, you must create an Outlook application object.您必须从 Excel 创建 Outlook 应用程序对象。 From Outlook, you are already within an Outlook application object so you do not need to create another.在 Outlook 中,您已经在一个 Outlook 应用程序对象中,因此您无需再创建另一个对象。 In fact, you cannot create a new Outlook application object because Outlook only allows one version of itself to be running.实际上,您无法创建新的 Outlook 应用程序对象,因为 Outlook 只允许运行其自身的一个版本。 If you try to create a new Outlook application object, you are given a reference to the copy that is already running.如果您尝试创建新的 Outlook 应用程序对象,您将获得对已运行副本的引用。 Again you may wish to record this information in a resource folder.同样,您可能希望将此信息记录在资源文件夹中。

I have used InputBox to obtain the four values from the user.我已经使用InputBox从用户那里获取了四个值。 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.本网站和其他地方有很多答案,展示了如何修改电子邮件的 html 或文本正文。 I assume you want to send html emails so I have amended the html body.我假设你想发送 html 电子邮件,所以我修改了 html 正文。

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.我的宏会显示编辑过的电子邮件,但如果您不想在电子邮件发送前检查电子邮件,则可以使用“ Send而不是“ Display

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM