首页 文章

从Excel VBA编辑Outlook电子邮件

提问于
浏览
1

我已经得到以下代码来成功使用在我的机器上本地保存的预制Outlook模板(fileName)并将Active Excel文档附加到其中,但是我还想添加一些其他文本 . 电子邮件模板,以节省我复制和粘贴它的时间 . 反正有没有将ADDITIONAL正文添加到预制的电子邮件模板中,或者如果我可以让我的VBA代码读取正文,然后我可以通过将其存储在临时变量中来添加它?这是一个保存的.msg文件

Public Function GenerateEmail(sendTo As String, _
    sendCC As String, sendBCC As String, _
    subjectText As String, fileName As String)

    Application.ScreenUpdating = False

    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItemFromTemplate(fileName)

    With OutMail
        .sendTo = sendToText
        .CC = sendCCText
        .BCC = sendBCCText
        .Subject = subjectText
        .Attachments.Add (Application.ActiveWorkbook.FullName)
        .Display
    End With

    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

End Function

2 回答

  • 1

    您必须保存模板 - 您只需草拟一个模板(我通常使用标记来替换正文的某些部分,具体取决于收件人 - 如"Dear %Recipient%"),然后使用"Save as" a .oft文件 . 然后运行代码发送邮件 . 我还会使用 .HTMLbody 来保持模板的格式化,这样你就可以放入

    With OutMail
            .sendTo = sendToText
            .CC = sendCCText
            .BCC = sendBCCText
            .Subject = subjectText
            .HTMLbody= WorksheetFunction.Substitute(OutMail.HTMLbody, "%Recipient%", [Recipiants name here (this could be a stored string)])
            .Attachments.Add (Application.ActiveWorkbook.FullName)
            .Display
    End With
    
  • 0

    只需在电子邮件文本中添加.body,如下所示

    With OutMail
            .sendTo = sendToText
            .CC = sendCCText
            .BCC = sendBCCText
            .Subject = subjectText
            .body = "Add your Text here"
            .Attachments.Add (Application.ActiveWorkbook.FullName)
            .Display
    End With
    

相关问题