简体   繁体   English

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

[英]Formatting an Excel VBA Email with Variables

I am trying to send an automated email with many variable included. 我正在尝试发送包含许多变量的自动电子邮件。 After a bunch of data analysis, i'm using a collection to distribute a number of emails including custom variable. 经过一堆数据分析后,我正在使用一个集合来分发许多电子邮件,包括自定义变量。 I'd like to bold, highlight and format the email. 我想加粗,突出显示和格式化电子邮件。 Unfortunately I only have one string to work from. 不幸的是,我只有一个字符串可以工作。 I tried making a Sub just for formatting my email. 我尝试制作Sub,仅用于格式化电子邮件。 How do I apply this bold to the email with variables throughout? 如何将此粗体应用于带有变量的电子邮件?

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

Send Code 发送代码

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

You need to set the .BodyFormat and .HTMLBody of the OutMail object. 您需要设置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>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM