2017-07-20 14 views
0

多くの変数を含む自動メールを送信しようとしています。データ分析の束の後、私はカスタム変数を含む電子メールの数を配布するためにコレクションを使用しています。私は大胆に、強調表示し、電子メールをフォーマットしたいと思います。残念ながら、私は1つの文字列しか扱えません。私はちょうど私の電子メールをフォーマットするためのSubを作ってみました。この太字を変数全体の電子メールにどのように適用すればよいですか?変数付きExcel VBA電子メールの書式設定

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 [email protected]" & 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 

答えて

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>" 
関連する問題