【问题标题】:How to create an email based on a template?如何基于模板创建电子邮件?
【发布时间】:2018-09-25 22:27:11
【问题描述】:

我尝试将电子邮件设置为函数,但我达到了最大回车数。

我想将电子邮件设为模板,以便更轻松地更新然后发送。

我所拥有的适用于较短的消息。我的信息很长,所以我认为最好使用模板。

Sub Sample_Auto_Generated_Email()
    Dim Email_Subject, Email_Send_From, Email_Send_To, _
    Email_Cc, Email_Body As String
    Dim Mail_Object, Mail_Single As Variant
    
    Email_Subject = "Sample MAR Email"
    
    Email_Send_From = "Richard.Sabbara@x.com"
    
    Email_Send_To = "Richard.Sabbara@x.com"
    Set objOutl = CreateObject("Outlook.Application")
    Set objMailItem = objOutl.CreateItem(olMailItem)
    
    objMailItem.Display
    strEmailAddr = "Richard.Sabbara@x.com"
    objMailItem.Recipients.Add strEmailAddr
    objMailItem.Subject = "Sample"
    objMailItem.Body = GetMessageBody()  
    objMailItem.Send
    Set objMailItem = Nothing
    Set objOutl = Nothing
End Sub


' This Function has been added.
Function GetMessageBody() As String
    GetMessageBody = "Good Afternoon" & vbNewLine & _
        Chr(10) & _
        "Attached is your Monthly Action Report (MAR) for May 2018." & vbNewLine & _
        Chr(10) & _
        "This report has been password protected with your practice password provided         
        to you by your ACOP Care Coordinator in April 2018." & vbNewLine & _
        Chr(10) & _
        "Technical questions (such as, how to access the MAR, password issues, not 
        receiving the email, etc.), please contact the Physician Engagement team at 
        providerservices@ax.com."
        Chr (10) & _
        "Questions related to the patient data contained within the MAR, please `enter code here`
        contact your ACO Partner Care Coordinator."
        Chr (10) & _
        "Thank you,"
End Function

【问题讨论】:

  • 代码有效,我希望它发送电子邮件模板而不是嵌入式消息。如果我的模板名为 temp.oft,我希望它为消息打开它。
  • 我找到了以下代码并打开了模板,我只需要添加收件人、发件人、抄送、主题行和附件。 Sub OutlookTemplate1() Dim myolapp As Object Dim myitem As Object Set myolapp = CreateObject("Outlook.Application") myolapp.Session.Logon '修改文件的链接和标题以使用您的示例 Set myitem = myolapp.CreateItemFromTemplate(" C:\Users\e4h7x8r\Desktop\Test_Templates.oft") myitem.Display '或发送 End Sub
  • 你不应该在这里使用函数
  • 您可能只想使用内置的template 并使用vba 来启动它

标签: excel vba templates outlook


【解决方案1】:

你可以创建一个后缀名为.docx的文档,把你的模板放在这个文档中。

将您的代码替换为以下代码:

Sub Sample_Auto_Generated_Email()
Dim Email_Subject, Email_Send_From, Email_Send_To, _
Email_Cc, Email_Body As String
Dim Mail_Object, Mail_Single As Variant
Dim wd As Object, editor As Object
Dim doc As Object
Dim oMail As MailItem

    Set wd = CreateObject("Word.Application")
    Set doc = wd.documents.Open("D:\aa.docx")
    doc.Content.Copy
    doc.Close
    Set wd = Nothing
    Email_Subject = "Sample MAR Email"

    Email_Send_From = “"

    Email_Send_To = ""
    Set objOutl = CreateObject("Outlook.Application")
    Set objMailItem = objOutl.CreateItem(olMailItem)
    objMailItem.Display
    strEmailAddr = ""
    objMailItem.Recipients.Add strEmailAddr
    objMailItem.Subject = "Sample"
    objMailItem.BodyFormat = olFormatRichText
    Set editor = objMailItem.GetInspector.WordEditor
        editor.Content.Paste
    'objMailItem.HTMLBody =
    objMailItem.Send
    Set objMailItem = Nothing
    Set objOutl = Nothing

    End Sub

这是我的结果:

【讨论】:

  • 我同意使用一些外部位置来存储消息的方法更有意义
  • 我通过在每个函数中创建小的文本集来完成以下工作。它可能不是最干净的,但它可以工作。谢谢。Sub Sample_Auto_Generated_Email_Final() Set Mail_Object = CreateObject("Outlook.Application") With Mail_Object.CreateItem(o) .Subject = "MAR Report" .To = "Richard.Sabbara@x.com" .Body = GetMessageBody() & GetMessageBody2() .Attachments.Add "C:\Users\e4h7x8r\Desktop\History.txt" .Attachments.Add "C:\Users\e4h7x8r\Desktop\Dr. C Report.xlsx" .Send End With End Sub
猜你喜欢
  • 2021-08-10
  • 1970-01-01
  • 2013-09-01
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
  • 2020-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多