从Excel VBA编辑Outlook电子邮件 [英] Edit Outlook email from Excel VBA

查看:412
本文介绍了从Excel VBA编辑Outlook电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下代码,可以成功使用预先存储在本地计算机上的预制Outlook模板(文件名),并将Active Excel文档附加到其中,但是我想添加一些其他文本添加到电子邮件模板可以节省我复制和粘贴的时间.无论如何,是否可以将其他主体文本添加到预制的电子邮件模板中,或者如果我可以让我的VBA代码读取该主体文本,然后可以通过将其存储在临时变量中来对其进行添加?这是一个已保存的.msg文件

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

推荐答案

您必须保存一个模板-您只需草拟一个模板(根据接收者的不同,我通常使用标记来代替身体的某些部位,例如尊敬的%Recipient%),然后另存为" .oft文件.然后运行您的代码以发送邮件.我还可以使用.HTMLbody来维护模板的格式,以便您放入

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

这篇关于从Excel VBA编辑Outlook电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆