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

使用变量格式化Excel VBA电子邮件,vba,excel,email,Vba,Excel,Email,我正在尝试发送一封包含许多变量的自动电子邮件。经过一系列数据分析后,我使用一个集合来分发大量电子邮件,包括自定义变量。我想加粗,突出显示和格式的电子邮件。不幸的是,我只有一个字符串可以使用。我试着制作一个Sub,只是为了格式化我的电子邮件。如何将此粗体应用于始终带有变量的电子邮件 Sub FormatEmail() 'Introduction If ErrCount > 0 Or WarnCount > 0 Then EmailIntroduction

我正在尝试发送一封包含许多变量的自动电子邮件。经过一系列数据分析后,我使用一个集合来分发大量电子邮件,包括自定义变量。我想加粗,突出显示和格式的电子邮件。不幸的是,我只有一个字符串可以使用。我试着制作一个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
子格式电子邮件()
"导言"
如果ErrCount>0或WarnCount>0,则
EmailIntroduction=MyPeople(n).FirstName&“,您有”&ErrCount&“过期批次”和“&WarnCount&”警告!”
其他的
EmailIntroduction=MyPeople(n).FirstName&“,您所有的物品都很好。”
如果结束
端接头
发送代码

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
Sub-Mail\u-ActiveSheet()
“在Excel 2000-2016中工作
“有关提示,请参阅:http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
Dim FileExtStr作为字符串
Dim FileFormatNum尽可能长
将Sourcewb设置为工作簿
将WB设置为工作簿
Dim TempFilePath作为字符串
将文件名设置为字符串
Dim OutApp作为对象
将邮件变暗为对象
应用
.ScreenUpdate=False
.EnableEvents=False
以
设置Sourcewb=ActiveWorkbook
'将活动工作表复制到新工作簿
活动表,收到
设置Destwb=ActiveWorkbook
'确定Excel版本和文件扩展名/格式
用Destwb
如果Val(Application.Version)<12,则
“您使用的是Excel 97-2003
FileExtStr=“.xls”:FileFormatNum=-4143
其他的
“您使用的是Excel 2007-2016
选择Case Sourcewb.FileFormat
案例51:FileExtStr=“.xlsx”:FileFormatNum=51
案例52:
如果.hasvb项目
FileExtStr=“.xlsm”:FileFormatNum=52
其他的
FileExtStr=“.xlsx”:FileFormatNum=51
如果结束
案例56:FileExtStr=“.xls”:FileFormatNum=56
其他情况:FileExtStr=“.xlsb”:FileFormatNum=50
结束选择
如果结束
以
“”如果需要,请将工作表中的所有单元格更改为值
'与Destwb.Sheets(1)一起使用
“.Cells.Copy
'.Cells.paste特殊XLPaste值
“。单元格(1)。选择
"以
'Application.CutCopyMode=False
'保存新工作簿/发送/删除它
TempFilePath=Environ$(“temp”)和“\”
TempFileName=“部分”&Sourcewb.Name&“&Format(现在是“dd-mmm-yy h-mm-ss”)
Set-OutApp=CreateObject(“Outlook.Application”)
Set-OutMail=OutApp.CreateItem(0)
用Destwb
.SaveAs TempFilePath&TempFileName&FileExtStr,FileFormat:=FileFormatNum
出错时继续下一步
发邮件
.To=MyPeople(n).电子邮件
.CC=“”
.BCC=“”
.Subject=“个人批次状态分析:”&MyPeople(n).SiView
.Body=EmailIntroduction&vbNewLine&vbNewLine&“随附于此电子邮件的是您的个人批次处理状态。这是一个更新列表,日期为:“&Now&vbNewLine&vbNewLine&”请记住,批次在等待时间为:P0>6小时、P1>12小时、P4>4天和P9>30天时会被标记“&vbNewLine&vbNewLine&danger&vbNewLine&vbNewLine&vbNewLine&vbNewLine&“这是一封自动电子邮件。更新每天发送两次。如果您对该报告机制有任何意见或担忧,请发送电子邮件至Wesley.X。Sherow@us.tel.com.“&vbNewLine&”恭敬地说:韦斯利的机器人”
.Attachments.Add Destwb.FullName
'您也可以像这样添加其他文件
'.Attachments.Add(“C:\test.txt”)
.Send'或use.Display
以
错误转到0
.Close savechanges:=False
以
'删除已发送的文件
终止TempFilePath&TempFileName&FileExtStr
发送邮件=无
设置应用程序=无
应用
.ScreenUpdate=True
.EnableEvents=True
以
端接头

您需要设置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>"
Dim body_uu作为字符串
body=“你好,

”_ &“这是一条线

"_ &“这是另一行

"_ &“这是另一行

" .BodyFormat=2'olFormatHTML .HTMLBody=“&body&”