VBA发送带有发件人姓名的预先编写的电子邮件模板

时间:2018-05-10 10:12:16

标签: vba email outlook

我发送了很多带有附件的电子邮件,这些附件在文本正文中并不多见。 理想情况下,我想要一个快速输入以下内容的宏:

To FirstName

Please see attached.

Kind regards,

WillacyMe

所有电子邮件的结构均为FirstName.LastName@company.com。

我需要一种方法将FirstName添加到文本正文中。

我添加了以下打开模板的VBA代码:

Sub QuickAttachementTemplate()

    Set temp = Application.CreateItemFromTemplate("C:\Users\WillacyMe\AppData\Roaming\Microsoft\Templates\AttachmentTemplate.oft")
    temp.Display
    Set temp = Nothing

End Sub

这只是添加FirstName的空间,我必须在运行宏后添加电子邮件。

有没有办法在新电子邮件中输入电子邮件地址,然后运行宏?

1 个答案:

答案 0 :(得分:0)

将FirstName占位符替换为从电子邮件地址解析的文本。

Option Explicit

Sub ReplacePlaceholder()

    Dim tempReplacePlaceholder As MailItem
    Dim recipAddress As String
    Dim firstNameFromAddress As String
    Dim len_firstNameFromAddress As Long

    ' Generate mailitem with placeholder "FirstName"
    ' Enter a single recipient in the To field
    Set tempReplacePlaceholder = ActiveInspector.CurrentItem
    tempReplacePlaceholder.Save

    With tempReplacePlaceholder
        recipAddress = .To
        Debug.Print recipAddress

        ' All email addresses must be structured FirstName.LastName@company.com.
        ' Find first instance of "."
        len_firstNameFromAddress = InStr(recipAddress, ".") - 1
        Debug.Print len_firstNameFromAddress

        firstNameFromAddress = Left(recipAddress, len_firstNameFromAddress)
        Debug.Print firstNameFromAddress
        .Body = Replace(.Body, "FirstName", firstNameFromAddress)
        .Display

    End With

End Sub