我将我的电子邮件正文放在表格中以便轻松编辑内容。问题是我需要在主体内容中使用一些变量。
例如:
我使用下面的代码发送电子邮件。正如你所看到的,我的电子邮件正文来自我的表格中的备忘录字段(.HTMLBody = "" & Me.Html_Email_Body & ""
),我需要在Html_Email_Body
字段中使用一些变量,如下所示:
(这是我在备忘录字段中的文字)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head></head>
<body>
Hi " & Me:PersonName & ", how are you?
</body>
</html>
输出结果是:Hi " & Me.PersonName & ", how are you?
输出结果应为:Hi Bob, how are you?
这可能吗?
(这是我用来发送电子邮件的代码)
Sub SendEmail_Click()
Dim NewMail As CDO.Message
Set NewMail = New CDO.Message
'Enable SSL Authentication
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
'Make SMTP authentication Enabled=true (1)
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
'Set the SMTP server and port Details
'To get these details you can get on Settings Page of your Gmail Account
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Set your credentials of your Gmail Account
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "[email protected]"
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword7"
'Update the configuration fields
NewMail.Configuration.Fields.Update
'Set All Email Properties
With NewMail
Dim strPath As String
strPath = ".mysite/wp-content/uploads/2017/07/myimage.png"
.subject = "this is the subject"
.From = "<[email protected]>"
.To = Me.EMAIL
'.CC = ""
'.BCC = ""
.HTMLBody = "" & Me.Html_Email_Body & ""
.AddAttachment "https://mysite/temp/contacts.vcf"
End With
NewMail.Send
MsgBox ("This email was sent!")
'Set the NewMail Variable to Nothing
Set NewMail = Nothing
End Sub
我在很多应用程序中都是这样做的。我将字段引用插入到我的电子邮件模板中,然后使用我编写的例程在运行时使用正确的值动态替换它们。在我的情况下,这通常是在一个循环中完成的,RecordSet包含几个人,每个人都收到电子邮件的单独副本,我正在为每个收件人定制模板。
这是一个小样本电子邮件模板:
<p>Hello [RecipFirstName],</p> This auto-generated email has been sent to notify you that:
<h4>Approval Mailed is <b>LATE</b>.</h4>
Approval Mailed Date: [ApprovalMailed_Date]
[ProjectTemplate1]
然后我填写模板的代码如下:
'[mBody] is a string that will be the Body of the email
'[templateBody] is a string that was previously set to the email
' template for the message being processed
'[rstProject] is a DAO.RecordSet that was previously set to the
' required dataset for my purposes
'Notice that I use a combination of [Replace] functions and
' my custom function [sitsProcessFieldReferences] to update the
' template with the appropriate values.
'In my case, replace CRLF in the template with <BR>
mBody = Replace(templateBody, vbCrLf, "<BR>")
mBody = sitsProcessFieldReferences(mBody, rstProject)
'The following examples use variables I have already defined and
' populated to replace the field refernces.
mBody = Replace(mBody, "[RecipFirstName]", FirstName)
mBody = Replace(mBody, "[RecipLastName]", LastName)
mBody = Replace(mBody, "[RecipFullName]", FirstName & " " & LastName)
mBody = Replace(mBody, "[ProjectTemplate1]", pTemplate1)
最后是进行字段引用替换的函数。请注意,我有一个特殊情况,如果我在名称中使用“price”命名字段引用,我希望将替换值格式化为Currency。您可以针对任何情况自定义此代码。它只需要一些预先计划来为您的字段引用保持一致的命名约定。
此函数接受电子邮件模板(或任何文本字符串),并在其中搜索与RecordSet中的任何字段匹配的字段名称(括在方括号中),并将该引用替换为RecordSet中相应字段的值
Public Function sitsProcessFieldReferences(ByVal orgString As String, rstFields As DAO.Recordset) As String
On Error GoTo Err_PROC
Dim ErrMsg As String
Dim fld As DAO.Field
For Each fld In rstFields.Fields
If InStr(fld.Name, "price") Then
orgString = Replace(orgString, "[" & fld.Name & "]", Format(Nz(fld.Value, 0), "Currency"))
Else
orgString = Replace(orgString, "[" & fld.Name & "]", Nz(fld.Value, ""))
End If
Next fld
Set fld = Nothing
Exit_PROC:
sitsProcessFieldReferences = orgString
Exit Function
Err_PROC:
Select Case Err.Number
Case Else
ErrMsg = "Module: " & strModName & vbCrLf
ErrMsg = ErrMsg & "Error: " & Err.Number & vbCrLf
ErrMsg = ErrMsg & "Line: " & Erl() & vbCrLf
ErrMsg = ErrMsg & Err.Description
DoCmd.Hourglass False
MsgBox ErrMsg, vbOKOnly + vbCritical, "Function sitsProcessFieldReferences"
Resume Exit_PROC
Resume
End Select
End Function
在您的电子邮件模板中,您将更改以下行:
Hi " & Me:PersonName & ", how are you?
类似于:
Hi [PersonName], how are you?
如果你已经在变量或其他东西中有替换值,那么要么做一个Replace(emailTemplate, [PersonName], "Bob")
。
或者,如果值在RecordSet中,您将更改模板中的[PersonName]
以匹配RecordSet中包含值Bob
的字段的名称,然后使用我的自定义函数:sitsProcessFieldReferences(emailTemplate, YourRecordSet)
我设法自己找到解决方案,因为我无法实现@Jericho Johnson虽然它在某种程度上有用......
我所做的是为电子邮件正文设置一个新变量(MyHTMLBody
),并根据需要设置几个替换(见下文)。
之后,我以这种方式设置.HTMLBody = MyHTMLBody
,现在我可以在HTML中使用这样的书签:Hi [r_name], how are you? This is your [r_email].
MyHTMLBody = Me.Body
MyHTMLBody = Replace(MyHTMLBody, "[r_name]", Me.Client_Name)
MyHTMLBody = Replace(MyHTMLBody, "[r_email]", Me.Client_Email)
.HTMLBody = MyHTMLBody