【问题标题】:Send query result in Outlook Email with Number formatted table使用数字格式的表格在 Outlook 电子邮件中发送查询结果
【发布时间】:2021-10-02 13:08:23
【问题描述】:

我想在 Outlook 中发送一封电子邮件,其中包含我从 Access 获得的查询结果。电子邮件的正文包括一个带有结果的表格(列/行)。当值为数字时,我想使用带逗号 xx,xxx 的数字格式。

我回收了我在这里找到的这段代码。如何格式化表格输出?

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

    'Create the header row
    aHead(1) = "Date"
    aHead(2) = "Request Type"
    aHead(3) = "Total" 'I want this to be comma separate number format?

    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")
            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

【问题讨论】:

    标签: vba ms-access formatting html-email


    【解决方案1】:

    试试这样的:

                aRow(1) = Format(rec("YourDateField"), "yyyy-mm-dd")
                aRow(2) = rec("YourRequestType")
                aRow(3) = Format(rec("YourTotal"), "0.000")
    

    如果留下一个点作为小数分隔符,请尝试:

                aRow(3) = Replace(LTrim(Str(rec("YourTotal"))), ".", ",")
    

    如果逗号是千位分隔符,请尝试:

                aRow(3) = Format(rec("YourTotal"), "00,000")
    

    【讨论】:

    • 谢谢!效果很好。我想这很简单。当我创建标题时,我一直在尝试进行格式化,现在这一切都说得通了。
    • 太棒了!那么请标记为已回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 2020-01-12
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多