带变量的电子邮件模板

时间:2017-09-07 07:03:55

标签: outlook-vba

我使用在此论坛的帮助下编写的代码,通过下拉菜单从中央存储库中选择.oft电子邮件模板。

Public Sub Email_Templates()

  With Select_Email_Template
    .Show
    If .Tag <> -1 Then
      CreateItemFromTemplate(Templates(.Tag, FullPath:=True)).Display ' Templates(.Tag) also works
    End If
  End With
  Unload Select_Email_Template

End Sub

Public Function Templates _
                ( _
                  Optional ByVal plngIndex As Long = -1 _
                , Optional ByVal NameOnly As Boolean = False _
                , Optional ByVal FullPath As Boolean = False _
                ) As Variant

  Const strcTemplatesDir As String = "\\c\s\caf1\Digital Delivery Group\DDCOPS\Splunk\Email Templates\"
  Const strcTemplateExtension As String = ".oft"

  Static avarTemplateNames As Variant

  If IsEmpty(avarTemplateNames) Then
    avarTemplateNames = Array _
    ( _
      "Account Amendment Non SC" _
    , "Amendment SC Application Received" _
    , "Amendment SC" _
    , "Creation Non SC" _
    , "Account Creation SC Application Received" _
    , "Account Creation SC" _
    , "Export Function" _
    , "Password Reset" _
    )
  End If
  If plngIndex <> -1 Then
    If NameOnly = True And FullPath = False Then
      Templates = avarTemplateNames(plngIndex)
    Else
      Templates = strcTemplatesDir & avarTemplateNames(plngIndex) & strcTemplateExtension
    End If
  Else
    Templates = avarTemplateNames
  End If

End Function

这是模板的示例:

  

嗨,

     

我被要求在下面为您创建Splunk帐户   环境(多个)。

     

您的用户名和密码详情如下:

     

用户名:密码:

     

非常感谢和亲切的问候

我希望在创建电子邮件之前动态添加用户名和密码。

我真的不想要硬编码&#39; VBA代码中的电子邮件内容,因为这些模板由多个用户使用,因此如果需要对其进行更改,则必须多次更改。

关于最佳前进方向的任何建议?

1 个答案:

答案 0 :(得分:0)

您可以传递值并替换如下所示的唯一占位符:

Option Explicit

Private Sub userpass()

Dim userName As String
Dim userPassword As String

userName = "someBody"
userPassword = "easilyBroken"

MyTemplate userName, userPassword

End Sub

Private Sub MyTemplate(uName, uPword)

Dim msg As mailitem

' Template with unique text "Username:" and "Password:"
Set msg = CreateItemFromTemplate("C:\Test\Account Creation SC.oft")

With msg
    .body = Replace(.body, "Username:", "Username:" & uName)
    .body = Replace(.body, "Password:", "Password:" & uPword)
End With

msg.Display

End Sub