使用HTMLbody

时间:2015-11-25 20:02:14

标签: html excel excel-vba outlook vba

enter image description here Sub ColdEmail()

Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim lastrow As Long
Dim iCounter As Long
Dim MailDest As String
Dim subj As String
Dim bod As String
Dim ws As Worksheet
Dim signature As String

lastrow = ThisWorkbook.Worksheets("Prospects").Cells(Rows.Count, "D").End(xlUp).Row 'change worksheet

For iCounter = 2 To lastrow

    Set OutLookApp = CreateObject("Outlook.application")
    Set OutLookMailItem = OutLookApp.CreateItem(0)

     signature = Environ("appdata") & "\Microsoft\Signatures\"
    If Dir(signature, vbDirectory) <> vbNullString Then
        signature = signature & Dir$(signature & "*.htm")
    Else:
        signature = ""
    End If
    signature = CreateObject("Scripting.FileSystemObject").GetFile(signature).OpenAsTextStream(1, -2).ReadAll

    With OutLookMailItem

        subj = ""
        MailDest = ""
        bod = ""

        If Cells(iCounter, 13) = "*" Then

            subj = Cells(iCounter, 14).Value
            MailDest = Cells(iCounter, 7).Value
            bod = Cells(iCounter, 16).Value

            .BCC = MailDest
            .Subject = subj
            .HTMLBody = bod & signature
            .Send
        End If

    End With



Next iCounter

End Sub

上面的代码会自动将电子邮件发送到一列电子邮件地址,并从Excel中的列中获取正文段落。

我希望我的邮件在Outlook中包含我的默认签名,因此我将代码更改为HTMLbody。

发出的电子邮件不保留原始段落间距:

line 1

line 2

line 3

现在看起来像这样:line1 line2 line3

2 个答案:

答案 0 :(得分:2)

我会回应Scott Holtzman的建议来阅读HTML。每当我需要在电子邮件中进行HTML格式化时,我发现<br>是最简单的选择。使用一个<br>会将文本移动到下一行。要创建空白区域(例如,段落之间的空白行),请使用<br><br>

您可以将此权限放入文本所在的单元格中。括号将此标记为HTML,它将作为中断而不是可读文本在电子邮件中呈现。

换句话说:excel单元格中的Thanks,<br> Cthulhu将呈现为
“谢谢你,克苏鲁”在你的电子邮件中。

答案 1 :(得分:1)

阅读coding HTML

目前,你可以这样做:

要加载默认签名(已在outlook中设置),您可以执行以下操作(并删除代码以获取签名):

With OutLookMailItem
     .Display
     signature = .HTMLBody
     ....

要格式化正文的HTML,您可以执行以下操作:

'change font info as needed
bod = "<BODY style=font-size:12pt font-family:Times New Roman font-color:blue>" _
      & "<p>" & Cells(iCounter, 16).Value & "</p>" _
      & "</BODY>" _
      & signature