使用变量格式化Excel VBA电子邮件

时间:2017-07-20 17:26:51

标签: vba excel-vba email excel

我正在尝试发送包含许多变量的自动电子邮件。经过一系列数据分析后,我使用一个集合来分发包括自定义变量在内的大量电子邮件。我想加粗,突出显示并格式化电子邮件。不幸的是我只有一个字符串可供使用。我尝试制作Sub只是为了格式化我的电子邮件。如何将这个粗体应用于包含变量的电子邮件?

Sub FormatEmail()
    'Introduction
    If ErrCount > 0 Or WarnCount > 0 Then
        EmailIntroduction = MyPeople(n).FirstName & ", you have <strong>" & ErrCount & " EXPIRED LOTS</strong> and " & WarnCount & " warnings!"
    Else
        EmailIntroduction = MyPeople(n).FirstName & ", all of your lots are good."
    End If

End Sub

发送代码

Sub Mail_ActiveSheet()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook

    'Copy the ActiveSheet to a new workbook
    ActiveSheet.Copy
    Set Destwb = ActiveWorkbook

    'Determine the Excel version and file extension/format
    With Destwb
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007-2016
            Select Case Sourcewb.FileFormat
            Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
            Case 52:
                If .HasVBProject Then
                    FileExtStr = ".xlsm": FileFormatNum = 52
                Else
                    FileExtStr = ".xlsx": FileFormatNum = 51
                End If
            Case 56: FileExtStr = ".xls": FileFormatNum = 56
            Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
            End Select
        End If
    End With

    '    'Change all cells in the worksheet to values if you want
    '    With Destwb.Sheets(1).UsedRange
    '        .Cells.Copy
    '        .Cells.PasteSpecial xlPasteValues
    '        .Cells(1).Select
    '    End With
    '    Application.CutCopyMode = False

    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .To = MyPeople(n).Email
            .CC = ""
            .BCC = ""
            .Subject = "Personal Lot Status Analysis: " & MyPeople(n).SiView
            .Body = EmailIntroduction & vbNewLine & vbNewLine & "Attached to this email is your personal lot processing status. This is an updated list as of: " & Now & vbNewLine & vbNewLine & "Remember that lots are flagged when on hold for: P0>6Hrs, P1>12Hrs, P4>4Days, and P9>30Days" & vbNewLine & vbNewLine & danger & vbNewLine & vbNewLine & "This is an automated email. Updates are sent two times per day. If you have comments or concerns regarding this reporting mechanism, please email Wesley.X.Sherow@us.tel.com." & vbNewLine & "Respectfully: Wesley's Robot"
            .Attachments.Add Destwb.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Send   'or use .Display
        End With
        On Error GoTo 0
        .Close savechanges:=False
    End With

    'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

您需要设置OutMail对象的.BodyFormat.HTMLBody

Dim body_ As String
    body_= "<p> Hello, </p>" _
         & "<p> This is a line. </p>" _
         & "<p> This is another line. </p>" _
         & "<p> This is <b>yet</b> another line. </p>"

.BodyFormat = 2    'olFormatHTML
.HTMLBody = "<html><head></head><body>" & body_ & "</body></html>"