首页 > 解决方案 > 在邮件的 htmlbody 中包含 Excel 范围值

问题描述

我正在尝试在 .htmlbody 中包含 Range("F2").Value。

signature = opemail.htmlbody '

With opemail
    .to = "s"
    .Subject = ActiveCell.Value & " - " & "Daily report - " & Date - 1
    .htmlbody = "<p style=""font:11pt Calibri;""> **Range("F2").Value** <br> more text </p>" & signature

单元格“F2”有一个随时间变化的文本(早上好 <12,>12 下午好)。

我的英语很基础。

标签: vbaexceloutlook

解决方案


您可以使用问题中所述的单元格引用,您只需要使用&符号来链接字符串:

.htmlbody = "<p style=""font:11pt Calibri;"">" & Range("F2").Value & "<br> more text </p>"

或者您可以在 VBA 中执行此操作而不使用单元格引用:

        Dim CurTime
            If Hour(Now) < 12 Then
                    CurTime = "Good Morning"
                Else
                    CurTime = "Good Afternoon"
            End If

        signature = opemail.htmlbody

        With opemail
            .to = "s"
            .Subject = ActiveCell.Value & " - " & "Daily report - " & Date - 1
            .htmlbody = "<p style=""font:11pt Calibri;"">" & CurTime & "<br> more text </p>"

推荐阅读