vba 使用 Excel Visual Basic 的电子邮件正文中的粗体变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22851554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bold Variables in Email body using Excel Visual Basic
提问by KKW
This is actually my first time posting on this site - I really appreciate all the help I can get!
这实际上是我第一次在这个网站上发帖 - 我真的很感激我能得到的所有帮助!
I have an Excel sheet that contains
我有一个 Excel 工作表,其中包含
- clients emails,
- their name,
- their check dates and
- and their processing date.
- 客户电子邮件,
- 他们的名字,
- 他们的检查日期和
- 和他们的处理日期。
I want to send out a reminder email to them daily if their processing date is today. I've been successful in writing the code - but I haven't been able to bold parts of the email (ProcessingDate
, CheckDate
and Time
).
如果他们的处理日期是今天,我想每天向他们发送一封提醒电子邮件。我已经成功地编写了代码 - 但我无法将电子邮件的部分加粗(ProcessingDate
、CheckDate
和Time
)。
Thank you so much again!
再次感谢你!
Here is my code:
这是我的代码:
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
采纳答案by brettdj
You can use
您可以使用
.htmlBody
rather than.Body
- use html
.htmlBody
而不是.Body
- 使用 html
So to bold ProcessingDate
所以要大胆 ProcessingDate
Msg = Msg & "<b>" & ProcessingDate & "</b> "
Msg = Msg & "<b>" & ProcessingDate & "</b> "
For example with your code
例如使用您的代码
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 = "[email protected]"
.htmlBody = Msg
'.Send
.display 'disable display and enable send to send automatically
End With