我很难通过 Access VBA 将格式化表格放入电子邮件中。我可以让它为记录集中的所有内容创建一个巨大的表,但我需要为每个支票号(记录集中的一个字段)创建一个表。有什么想法吗?
我尝试了很多代码变体 - 我想我可能需要 arow(1-9) 中的 arow 是动态的,但我无法弄清楚这一点。
这是我必须制作一张巨大桌子的东西:
Dim olApp As Object
Dim olItem As Variant
Dim db As DAO.Database
Dim rec As DAO.Recordset
Dim strQry As String
Dim aHead(1 To 8) As String
Dim aRow(1 To 8) As String
Dim aBody() As String
Dim lCnt As Long
Dim strpicpath As String
Dim appoutlook As Outlook.Application: Set appoutlook = New Outlook.Application
Dim mimEmail As Outlook.MailItem
Set mimEmail = appoutlook.CreateItem(olMailItem)
'strpicpath = "path"
'Create the header row
aHead(1) = "End Debtor Name"
aHead(2) = "Invoice"
aHead(3) = "Check Number"
aHead(4) = "Exception Type"
aHead(5) = "Amount"
aHead(6) = "Actions to Resolve"
aHead(7) = "Notes"
aHead(8) = "Client Response"
lCnt = 1
ReDim aBody(1 To lCnt)
aBody(lCnt) = "<HTML><body><table border='2'><tr><th>" & Join(aHead, "</th><th>") & "</th></tr>"
'Create each body row
strQry = "SELECT * From tbl_SendEmailsTemp"
Set db = CurrentDb
Set rec = CurrentDb.OpenRecordset(strQry)
If Not (rec.BOF And rec.EOF) Then
Do While Not rec.EOF
lCnt = lCnt + 1
ReDim Preserve aBody(1 To lCnt)
aRow(1) = rec("EndDebtor Name")
aRow(2) = rec("Invoice #")
aRow(3) = rec("Check Number1")
aRow(4) = rec("Exception Type")
aRow(5) = rec("Balance")
aRow(6) = rec("Notes")
aRow(7) = ""
aRow(8) = ""
aBody(lCnt) = "<tr><td>" & Join(aRow, "</td><td>") & "</td></tr>"
rec.MoveNext
Loop
End If
aBody(lCnt) = aBody(lCnt) & "</table></body></html>"
'create the email
With mimEmail
.To = "test"
'.To = ContactEmail & ";" & ContactEmail2 & ";" & ContactEmail3
'.cc = creditrep & ";" & "credit "
.Subject = Client_Name & ", Exceptions Report, " & Date - 1
Dim att As Outlook.Attachment
Set att = .Attachments.Add(strpicpath, 1, 0)
.HTMLBody = "<img src=""Exceptions.png""><br><br><br>" _
& "<BODY style = font-size: 11pt>Please see below for the exceptions generated from " & Date - 1 & " transactions. All applicable back-up documentation is attached:</BODY><br><br>" _
& Join(aBody, vbNewLine) & " <br><br>"
.Display
End With
DoCmd.OpenQuery "qry_AppendEmailed"
DoCmd.OpenQuery "qry_AppendNotEmailed"
DoCmd.OpenQuery "qry_DeleteFromExceptions"
DoCmd.SetWarnings True
End Sub
您需要一个循环支票号码的外部记录集。为此使用 GROUP BY。
对于外部循环的每次迭代,打开一个内部记录集,为该数字创建表。
类似:
sNumber = "SELECT [Check Number1] AS CurrentNumber From tbl_SendEmailsTemp GROUP BY [Check Number1]"
Set recNumber = db.OpenRecordset(sNumber)
Do While Not recNumber.EOF
' assuming [Check Number1] is a number
lNumber = recNumber!CurrentNumber
' Now select only the rows for that number
strQry = "SELECT * From tbl_SendEmailsTemp WHERE [Check Number1] = " & lNumber
' Then proceed creating the table with the code you have already
Set rec = db.OpenRecordset(strQry)
' ... etc.
' append this table (perhaps with a heading for [Check Number1]) to a string variable for the HTML body
recNumber.MoveNext
Loop
' Now create the email, using the body you built above