在访问表字段中使用VBA变量

时间:2018-09-15 22:53:03

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

我将我的电子邮件正文放在桌子内,以方便地编辑内容。问题在于,该正文内容中需要使用一些变量。

例如:

我使用下面的代码发送电子邮件。如您所见,我的电子邮件正文来自表(.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") = "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

2 个答案:

答案 0 :(得分: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)

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

此函数获取电子邮件模板(或任何文本字符串),并在其中搜索与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)

答案 1 :(得分:1)

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