使用Excel Visual Basic在电子邮件正文中的粗体变量

时间:2014-04-04 00:44:35

标签: excel excel-vba outlook vba

这实际上是我第一次在这个网站上发帖 - 我非常感谢我能得到的所有帮助!

我有一张包含

的Excel表格
  • 客户电子邮件,
  • 他们的名字,
  • 他们的检查日期和
  • 及其处理日期。

如果他们的处理日期是今天,我想每天向他们发送一封提醒电子邮件。我已成功编写代码 - 但我无法加粗电子邮件的部分内容(ProcessingDateCheckDateTime)。

再次感谢你!

这是我的代码:

Sub SendEm()
Dim i As Integer, Mail_Object, Email_Subject, o As Variant, lr As Long, MyDate As Date, Client As String, ProcessingDate As Date, CheckDate As Date, Time As Date, PayrollSpecialist As String
Dim Msg As Variant

lr = Sheets("DataSheet").Cells(Rows.Count, "S").End(xlUp).row
Set Mail_Object = CreateObject("Outlook.Application")
MyDate = Date
For i = 2 To lr
    Client = Sheets("DataSheet").Range("S" & i).Value
    ProcessingDate = Sheets("DataSheet").Range("B" & i).Value
    CheckDate = Sheets("DataSheet").Range("C" & i).Value
    Time = Sheets("DataSheet").Range("A" & i).Value
    PayrollSpecialist = Sheets("DataSheet").Range("D" & i).Value

        If Sheets("DataSheet").Range("B" & i).Value = MyDate Then
            Msg = "Dear" & " " & Client
            Msg = Msg & Sheets("Email").Range("B2").Value
            Msg = Msg & ProcessingDate & " "
            Msg = Msg & Sheets("Email").Range("B3").Value
            Msg = Msg & CheckDate
            Msg = Msg & ". " & Sheets("Email").Range("B4").Value & " "
            Msg = Msg & Time
            Msg = Msg & " " & Sheets("Email").Range("B5").Value & Sheets("Email").Range("B6").Value & vbNewLine & PayrollSpecialist
            With Mail_Object.CreateItem(o)
                .Subject = Sheets("Email").Range("A2").Value
                .To = Sheets("DataSheet").Range("T" & i).Value
                .Body = Msg
                '.Send
                .display 'disable display and enable send to send automatically
            End With
        End If
Next i
    MsgBox "E-mail successfully sent", 64
    Application.DisplayAlerts = False
    Set Mail_Object = Nothing
End Sub

1 个答案:

答案 0 :(得分:1)

您可以使用

  1. .htmlBody而不是.Body
  2. 使用html
  3. 所以要加粗ProcessingDate

    Msg = Msg & "<b>" & ProcessingDate & "</b> "

    例如使用您的代码

             Msg = "Dear" & " " & Client
            'Msg = Msg & Sheets("Email").Range("B2").Value
            Msg = Msg & "<b>" & ProcessingDate & "</b> "
            'Msg = Msg & Sheets("Email").Range("B3").Value
            Msg = Msg & CheckDate
            'Msg = Msg & ". " & Sheets("Email").Range("B4").Value & " "
            Msg = Msg & Time
            'Msg = Msg & " " & Sheets("Email").Range("B5").Value & Sheets("Email").Range("B6").Value & vbNewLine & PayrollSpecialist
            With Mail_Object.CreateItem(o)
                .Subject = "tested"
                .To = "someone@hotmail.com"
                .htmlBody = Msg
                '.Send
                .display 'disable display and enable send to send automatically
            End With