早上好,
我希望有人可以帮助我。我对VBA编码并没有过多的经验,并且有一些事情需要做,说实话我不知道从哪里开始....我遇到了一些我可以使用的编码,它位于下面
Dim strSQL
Dim db As DAO.Database
Dim MailList As DAO.Recordset
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyBodyText As String
Dim MyDecision As String
Dim strReportName As String
Dim strEnroll As String
Dim strWho As String
Dim strEmail As String
Set fso = New FileSystemObject
Set MyOutlook = New Outlook.Application
Set db = CurrentDb()
Set MailList = db.OpenRecordset("qryEmail")
Subjectline$ = "Expiration Date" & " " & Date
Set MyMail = MyOutlook.CreateItem(olMailItem)
Do While Not MailList.EOF
MyBodyText = MailList("")
MailList.MoveNext
Loop
MyMail.To = "" & ""
MyMail.CC = CurrentUser() & ""
MyMail.Subject = Subjectline$
MyMail.Body = MyBodyText
MyMail.Display
strEmail = Now()
strWho = CurrentUser()
Set MyMail = Nothing
Set MyOutlook = Nothing
查询名为 qryemail ,这些是该查询中的表格
ColID,ColDate,公司名称,地址,地址1,县,镇,邮政编码
在上面的代码中,我将这些表名放在......
中非常感谢任何帮助
感谢 艾玛
答案 0 :(得分:0)
为了澄清,您尝试将查询结果通过电子邮件发送给许多人。正确?如果是这样,你就很近了。
看看这段代码。它演示了从查询中获取值的方法。要使用,请将文本<INSERT YOUR FIELD>
替换为查询中字段的名称。因此,如果您需要Companyname
,请将其替换为文本。
Do While Not MailList.EOF
If MyBodyText = "" Then
MyBodyText = MailList![<INSERT YOUR FIELD>]
Else
MyBodyText = MyBodyText & "," & MailList![<INSERT YOUR FIELD>]
End If
MailList.MoveNext
Loop
答案 1 :(得分:0)
您需要遍历查询的字段名称(ColID,ColDate,公司名称,地址,地址1,县,城镇,邮政编码),并将它们全部连接到电子邮件正文中。
要在电子邮件正文中实现以下所有目标:
ColID :(某些价值)
ColDate :(某些价值)
公司:(有些价值)
地址:(有些价值)
地址1 :(有些价值)
县:(有些价值)
镇:(有些价值)
邮政编码:(某些价值)
代码如下:
MailList.MoveLast
MailList.MoveFirst
MyBodyText = ""
Do While Not MailList.EOF
MyBodyText = MyBodyText & "ColID: " & MailList!ColID & vbNewLine
MyBodyText = MyBodyText & "ColDate: " & MailList!ColDate & vbNewLine
MyBodyText = MyBodyText & "Company: " & MailList!CompanyName & vbNewLine
MyBodyText = MyBodyText & "Address: " & MailList!address & vbNewLine
MyBodyText = MyBodyText & "Address1: " & MailList!address1 & vbNewLine
MyBodyText = MyBodyText & "County: " & MailList!county & vbNewLine
MyBodyText = MyBodyText & "Town: " & MailList!town & vbNewLine
MyBodyText = MyBodyText & "Postcode: " & MailList!postcode & vbNewLine
MailList.MoveNext
Loop
请注意,这会遍历记录集的所有记录(qryEmail)。如果这不是你的意图,可以考虑在dbOpenRecordset行中添加一个where子句来过滤或调整输出到特定记录或几条记录。