[英]How to bold Outlook text from dynamic Excel cells in VBA?
I want to format the dynamic cells in the body to be bold.我想将正文中的动态单元格格式化为粗体。
I want the email body to look like that:我希望电子邮件正文看起来像这样:
Dear User,亲爱的用户,
The following request is in I8.1 Check Resource Availability:以下请求在 I8.1 Check Resource Availability 中:
Test Project测试项目
The estimated effort below is called out in the request's ROM (in hours): 10 and duration (in business days): 2 Do you have a named resource I can put in for the effort called out?请求的 ROM 中列出了以下估计的工作量(以小时为单位): 10和持续时间(以工作日为单位): 2您是否有我可以为所调用的工作量投入的命名资源? Please provide me with an update at the earliest请尽快向我提供更新
Thank you and best regards,感谢你并致以真诚的问候,
Team.团队。
This code is working but I can't find a way to bold the desired values.此代码正在运行,但我找不到加粗所需值的方法。
Sub Sendmail()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("D").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*@?*.?*" And _
LCase(Cells(cell.Row, "E").Value) <> "0" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "Resources awaiting assignment"
.Body = "Dear " & Cells(cell.Row, "C").Value _
& ", " _
& vbNewLine & vbNewLine & _
"The following request is in I8.1 Check Resource Availability: " _
& vbNewLine & vbNewLine & _
Cells(cell.Row, "A").Value _
& vbNewLine & vbNewLine & _
"The estimated effort below is called out in the request's ROM (in hours): " & Cells(cell.Row, "E").Value _
& _
" and duration (in business days): " & Cells(cell.Row, "F").Value _
& vbNewLine & vbNewLine & _
"Do you have a named resource I can put in for the effort called out? Please provide me with an update at the earliest" _
& vbNewLine & vbNewLine & _
"Thank you and best regards, " & vbNewLine & _
"Team."
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
I checked some posts on formating body of an email in VBA (eg How to format email body? , formatting email body using VBA or Bolding Text with VBA ).我检查了一些关于在 VBA 中格式化电子邮件正文的帖子(例如, 如何格式化电子邮件正文? , 使用 VBA 格式化电子邮件正文或使用 VBA 粗体文本)。
Use .HTMLBody
instead of .Body
使用.HTMLBody
而不是.Body
Then you can use HTML - Change your vbNewLine
to <br>
and use <b> text here </b>
to make things bold.然后您可以使用 HTML - 将您的vbNewLine
更改为<br>
并<b> text here </b>
使用<b> text here </b>
使内容vbNewLine
。
BraX finished their answer, which contained all the important points, before me. BraX 在我面前完成了他们的回答,其中包含了所有要点。 Perhaps you need my longer answer.也许你需要我更长的答案。
Body
is the text body for an email. Body
是一个电子邮件的正文。 BodyHtml
is the Html body. BodyHtml
是 Html 正文。
For Html, whitespace is defined as any of carriage return, linefeed, tab, space and some other more obscure characters.对于 Html,空白被定义为回车、换行、制表符、空格和其他一些更模糊的字符中的任何一个。 Any string of whitespace is the equivalent of a single space.任何空格字符串都等效于单个空格。 So:所以:
", " & vbNewLine & vbNewLine & "The following request is in I8.1"
is the same as: ", The following request is in I8.1"
同: ", The following request is in I8.1"
because the string of whitespace characters - space newline newline - becomes a single space.因为空白字符的字符串 - 空格换行符换行符 - 变成了一个空格。 You need to start a paragraph with <P>.你需要用 <P> 开始一个段落。 A linebreak is created with <BR>.使用 <BR> 创建换行符。
The easiest, if old fashioned, way of formatting some text as bold is by enclosing it in <B>xxx</B>.将某些文本格式化为粗体的最简单(如果老式)方法是将其括在 <B>xxx</B> 中。
You have some double spaces in your text.您的文本中有一些双空格。 If you want extra spaces, you need to use a non-break space which you can specify with &NBSP;如果你想要额外的空格,你需要使用一个不间断的空格,你可以用 &NBSP; 指定它。
So try:所以尝试:
.BodyHtml = "<P>Dear " & Cells(cell.Row, "C").Value & ", " _
"<P>The following request is in I8.1 Check Resource Availability: " _
"<P><B>" & Cells(cell.Row, "A").Value & "</B>" _
"<P>The estimated effort below is called out in the request's ROM (in hours): &NBSP;" & _
"<B>" & Cells(cell.Row, "E").Value & "</B> and duration (in business days): &NBSP;" & _
"<B>" & Cells(cell.Row, "F").Value & "</B>" _
"<P>Do you have a named resource I can put in for the effort called out? " & _
"Please provide me with an update at the earliest" _
"<P>Thank you and best regards, <BR>" _
"<P>Team."
I have not tested this VBA specified Html.我还没有测试过这个 VBA 指定的 Html。 I hope there are no mistakes but I am sure you can see what I am attempting if there are mistakes.我希望没有错误,但我相信如果有错误,你可以看到我在尝试什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.