【问题标题】:Use VBA variables inside an Access table field在 Access 表字段中使用 VBA 变量
【发布时间】:2019-02-20 07:16:36
【问题描述】:

我将我的电子邮件正文放在一个表格中,以便轻松编辑内容。问题是我需要在正文内容中使用一些变量。

例如:

我使用下面的代码发送电子邮件。如您所见,我的电子邮件正文来自表中的备注字段 (.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 " &amp; Me.PersonName &amp; ", 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") = "mysite@gmail.com"

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 = "<mail@mysite.com>"
.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

【问题讨论】:

  • 那行不通。您需要使用 VBA 代码引用此人的姓名。
  • @Rene 你能详细说明一个例子吗?

标签: ms-access vba ms-access-2007


【解决方案1】:

我在许多应用程序中都做这种事情。我将字段引用插入到我的电子邮件模板中,然后使用我编写的例程在运行时用正确的值动态替换它们。在我的例子中,这通常是在一个包含几个人的 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)

最后是执行字段引用替换的函数。请注意,我有一个特殊情况,如果我在名称中使用“价格”命名字段引用,我希望将替换值格式化为货币。您可以针对任何情况自定义此代码。它只需要一些预先计划来为您的字段引用保持一致的命名约定。

此函数采用电子邮件模板(或任何文本字符串)并在其中搜索与 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 " &amp; Me:PersonName &amp; ", how are you?

类似于:

Hi [PersonName], how are you?

如果您的变量或其他东西中已有替换值,请执行Replace(emailTemplate, [PersonName], "Bob")

或者,如果值在 RecordSet 中,您将更改模板中的 [PersonName] 以匹配 RecordSet 中包含值 Bob 的字段名称,然后使用我的自定义函数:sitsProcessFieldReferences(emailTemplate, YourRecordSet)

【讨论】:

    【解决方案2】:

    我自己设法找到了解决方案,因为我无法实现@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 
    

    【讨论】:

    • 这正是 Jericho Johnson 向您建议的。
    • 实际上,Alex,如果您阅读我的整个解决方案,您会发现我提供了两个选项,一个用于处理从 RecordSet 一次全部替换多个值的自定义函数以及使用 Replace 函数。此外,我提供了建议的模板 html 重新格式化,以方便替换嵌入的参考字段。你可能不同意@peakpeak 和我的观点,但对他人无礼是没有必要的。
    • @JerichoJohnson 首先,您提供一个小模板:这是一个小示例电子邮件模板: 然后您放置 我的代码来填充模板,如下所示: 最后你展示了进行字段引用替换的函数。如果我没看错,您的答案(例如,现在),您必须放置(我填充模板的代码如下:),然后使用您的函数(执行字段引用替换的函数)来完成这项工作。我试图将两者结合起来,但这没有用。 (下面继续……)
    • @JerichoJohnson 我试图将两者结合起来,但这没有用。如果他们独立工作,请澄清...同时,我从不打算对任何人粗鲁,但我不能忍受没有任何理由拒绝投票的人,只是因为...我感谢您的帮助(就像我之前州),您的回答很有用,因此我投了赞成票。所以,如果你觉得我对你不礼貌,请接受我的道歉。
    • 我觉得您的解决方案直接来自我的回答。我感谢您的支持,但在我看来,您也应该“接受”答案,因为您使用我的答案中的信息直接解决了您的问题。您不需要第二种字段替换方法(从 RecordSet 填充)这一事实无关紧要。您的问题没有指定您将在哪里获取数据,因此我提供了两个选项。别往心里放。这就是我们学习的方式。祝你好运。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 2020-03-29
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多