【问题标题】:How to bold Outlook text from dynamic Excel cells in VBA?如何在 VBA 中将动态 Excel 单元格中的 Outlook 文本加粗?
【发布时间】:2020-01-29 03:01:45
【问题描述】:

我想将正文中的动态单元格设置为粗体。

我希望电子邮件正文如下所示:

尊敬的用户,

以下请求在 I8.1 检查资源可用性:

测试项目

在请求的 ROM 中调用以下估计工作量(以小时为单位):10 和持续时间(以工作日为单位):2 你有我可以投入的命名资源吗?请尽快给我更新

谢谢你和最好的问候,

团队。

此代码有效,但我找不到加粗所需值的方法。

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

我查看了一些关于在 VBA 中格式化电子邮件正文的帖子(例如 How to format email body?formatting email body using VBABolding Text with VBA)。

【问题讨论】:

    标签: excel vba email outlook


    【解决方案1】:

    使用.HTMLBody 而不是.Body

    然后您可以使用 HTML - 将您的 vbNewLine 更改为 &lt;br&gt; 并使用 &lt;b&gt; text here &lt;/b&gt; 使内容加粗。

    【讨论】:

    • 感谢您的回答@braX,我知道如何将标准文本加粗,但正如我在帖子中提到的那样,我正在努力将“动态”单元格加粗...
    • “动态”是指Cells(cell.Row, "A").Value吗? - 你就这样做 - "&lt;b&gt;" &amp; Cells(cell.Row, "A").Value &amp; "&lt;/b&gt;" - 完全一样的事情
    【解决方案2】:

    BraX 在我面前完成了包含所有要点的答案。也许你需要我更长的答案。

    Body 是电子邮件的正文。 BodyHtml 是 Html 正文。

    对于 Html,空白定义为回车、换行、制表符、空格和其他一些更晦涩的字符。任何一个空格字符串都相当于一个空格。所以:

    ", " & vbNewLine & vbNewLine & "The following request is in I8.1"
    

    等同于:", The following request is in I8.1"

    因为空白字符的字符串——空格换行符换行符——变成了一个空格。您需要以

    开始一个段落。使用
    .

    创建换行符

    将某些文本格式化为粗体的最简单(如果是老式的)方法是将其包含在 xxx 中。

    您的文本中有一些双空格。如果你想要额外的空格,你需要使用一个不间断的空格,你可以用 &NBSP;

    指定

    那就试试吧:

    .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."
    

    我没有测试过这个 VBA 指定的 Html。我希望没有错误,但如果有错误,我相信您可以看到我在尝试什么。

    【讨论】:

    • 谢谢托尼!此外,您在结尾处遗漏了一些 &amp;,而文本中的 &amp;NBSP; 只是按原样显示 - 非常好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 2012-07-23
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多