【问题标题】:Send an outlook email to each record in the table in a table format以表格格式向表格中的每条记录发送outlook邮件
【发布时间】:2023-01-10 13:33:37
【问题描述】:

我有一个 MS Access 表,想以表格格式向表中的每条记录发送电子邮件。对于同一个城市或国家的表中的行,我希望 outlook 电子邮件将这些记录放在一个表中。

我包含了一个虚拟表DummyTable,我将使用 SQL 查询检索其记录:SELECT * FROM DummyTable 我已经使用我在这里找到的这个解决方案在 vba 中创建了表 access-vba-to-send-query-results-to-outlook-email-in-table-format

Public Sub NewEmail()

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 7) As String
Dim aRow(1 To 7) As String
Dim aBody() As String
Dim lCnt As Long

'Create the header row
aHead(1) = "Request Type"
aHead(2) = "ID"
aHead(3) = "Title"
aHead(4) = "Requestor Name"
aHead(5) = "Intended Audience"
aHead(6) = "Date of Request"
aHead(7) = "Date Needed"

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 Email_Query"
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("Test1")
        aRow(2) = rec("Test2")
        aRow(3) = rec("Test3")
        aRow(4) = rec("Test4")
        aRow(5) = rec("Test5")
        aRow(6) = rec("Test6")
        aRow(7) = rec("Test7")
        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
Set olApp = CreateObject("Outlook.application")
Set olItem = olApp.CreateItem(0)

olItem.display
olItem.To = "example@example.com"
olItem.Subject = "Test E-mail"
olItem.htmlbody = Join(aBody, vbNewLine)
olItem.display

End Sub

如果我想向表中的国家/地区为 TRNC Country "TRNC" 的姓名发送电子邮件,而对于英国郡,则为电子邮件正文提供单独的表,如 Country "UK",我理想的解决方案是将此表作为电子邮件正文。

我的方法是创建一个数组来保存 DummyTable 中的记录,然后遍历每条记录并为具有相同国家/地区名称的记录创建一个表,但我的实现失败了,我已经用了 2 周了。

【问题讨论】:

  • 表格图像中的数据甚至与代码中引用的字段名称不匹配。看起来您没有为您的数据库定制代码示例做太多工作。数据应以文本表格而非图像形式提供。
  • 所以您想将相同的记录发送给 Adam 和 Mina?与 Graeme 和 Briann 的记录相同?好吧,如果他们看到彼此的电子邮件,或者这应该是密件抄送地址吗?
  • 确切地!应将相同的记录发送给 Adam 和 Mina。 Graeme 和 Briann 也有同样的记录

标签: vba ms-access outlook html-email office-automation


【解决方案1】:

根据给定的样本数据,考虑:

Public Sub NewEmail()

Dim olApp As Object
Dim olItem As Variant
Dim rec As DAO.Recordset
Dim aRow(1 To 5) As String
Dim aBody() As String
Dim lCnt As Long
Dim strCountry As String
Dim strHTML As String
Dim strEmail As String
Dim x As Integer

strHTML = "<HTML><body><table border='2'><tr><th>"
strHTML = strHTML & "Name</th><th>"
strHTML = strHTML & "Country</th><th>"
strHTML = strHTML & "City</th><th>"
strHTML = strHTML & "School</th><th>"
strHTML = strHTML & "Email</th>"
strHTML = strHTML & "XXX</table></body></html>"

lCnt = 1

Set olApp = CreateObject("Outlook.application")

Set rec = CurrentDb.OpenRecordset("SELECT * FROM DummyTable ORDER BY Country, City, School, Name")

If Not (rec.BOF And rec.EOF) Then
    strCountry = rec!Country
    For x = 1 To rec.recordCount
        lCnt = lCnt + 1
        ReDim Preserve aBody(1 To lCnt)
        aRow(1) = rec("Name") & "</td><td>"
        aRow(2) = rec("Country") & "</td><td>"
        aRow(3) = rec("City") & "</td><td>"
        aRow(4) = rec("School") & "</td><td>"
        aRow(5) = rec("Email") & "</td>"
        aBody(lCnt) = "<tr><td>" & Join(aRow, "") & "</tr>"
        strEmail = strEmail & rec!Email & ";"
        If x < rec.recordCount Then rec.MoveNext
        If x = rec.recordCount Or strCountry <> rec!Country Then
            'create the email
            Set olItem = olApp.CreateItem(0)
            olItem.Display
            olItem.To = "my email"
            olItem.BCC = strEmail
            olItem.Subject = "Test E-mail"
            olItem.HTMLBody = Replace(strHTML, "xxx", Join(aBody, vbNewLine))
            olItem.Display
            lCnt = 0
            strEmail = ""
            strCountry = rec!Country
        End If
    Next
End If

End Sub

【讨论】:

  • 谢谢你。我根据您的解决方案实现了代码。我遇到“End if without block if”错误。我还尝试了不同的调试解决方案,例如在 for 循环块下构建 if 语句,但都无济于事。
  • 第一个“End If”是在调试模式下突出显示的行
  • 代码非常适合我。
【解决方案2】:

获取查询数据并在 html 正文电子邮件中以表格形式显示的优秀代码。

我只想进一步了解,是否可以在正文中显示每条记录的情况下在附加列中显示图片?图片存储在共享文件夹中的某个位置。 谢谢,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 2020-11-08
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多