格式化Excel值输出到Outlook

时间:2015-10-07 16:34:12

标签: excel vba excel-vba outlook

我试图创建一个从Excel抓取信息并将其输出到Outlook的工具。

到目前为止一直很好,但我无法在电子邮件中显示格式化的数值。它将其显示为普通数字,例如203486442,但我希望它是$203,486,441.93。我得到了一个"无效的资格赛"尝试格式化时出错。这个数字来自" Total"我的工作表中的表行。

Sub Test1()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range

Set OutApp = CreateObject("Outlook.Application")

Dim LastRow As Long
Dim LastValue As Long
LastRow = Sheets("Detail Aging").Range("E" & Rows.Count).End(xlUp).Row
LastValue = Sheets("Detail Aging").Range("E" & LastRow).Value

Dim strbody As String
For Each cell In Worksheets("Body").Range("B2:B6")
    strbody = strbody & cell.Value & vbNewLine
Next

On Error GoTo cleanup
For Each cell In Columns("D").Cells.SpecialCells(xlCellTypeConstants)
    If cell.Value Like "?*@?*.?*" And _
       LCase(Cells(cell.Row, "E").Value) = "yes" Then

        Set OutMail = OutApp.CreateItem(0)
        On Error Resume Next
        With OutMail
            .To = cell.Value
            .CC = Cells(cell.Row, "G").Value
            .Subject = "CompuCom Reminder Letter | " & Cells(cell.Row, "B").Value
            .Body = "Dear " & Cells(cell.Row, "C").Value & "," _
                & vbNewLine & vbNewLine & _
                "This is a friendly reminder that the " & _
                "total balance due amount on your account is " & LastValue _
                & vbNewLine & vbNewLine & _
                "Here is the list of outstanding transactions:" _
                & vbNewLine & vbNewLine & _
                "Please let us know what the payment status is and if you require additional " & _
                "information or documentation to process the invoice(s) for payment." _
                & vbNewLine & vbNewLine & _
                "Thank you." _
                & vbNewLine & vbNewLine & _
                Cells(cell.Row, "F").Value & vbNewLine & _
                Cells(cell.Row, "G").Value & vbNewLine & _
                Cells(cell.Row, "H").Value & vbNewLine & _
                "Regards,"
            .Display
        End With

        On Error GoTo 0
        Set OutMail = Nothing

    End If

Next cell

1 个答案:

答案 0 :(得分:1)

尝试使用单元格的.Text属性而不是.Value

您需要更改Lastvalue的声明,请尝试:

Dim lastValue As String ' or As Variant