Edit Outlook email from Excel VBA

喜你入骨 提交于 2019-12-13 19:43:05

问题


I've got the below code to successfully use a pre-made Outlook template that is saved locally on my machine (fileName) and attach the Active Excel document into it, however there is some additional text that I'd like to add to the email template to save me the time to copy and paste it over. Is there anyway to add ADDITIONAL body text to the pre-made email template or if I can have my VBA code read the body text and then I can add to it by storing it in a temporary variable? It's a saved .msg file

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

回答1:


You have to save down a template - you just draft a template (I typically use markers to substitute certain part of the body, depending on the recipient - like "Dear %Recipient%") and then "Save as" a .oft file. then run you code to sent the mails. I'd also use .HTMLbody to maintain the fomatting of the template so you'd put in

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



回答2:


Just add a .body to the email text such as below

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


来源:https://stackoverflow.com/questions/46733209/edit-outlook-email-from-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!