首页 > 解决方案 > 如何使用 VBA 从 Access 向 Outlook 邮件添加 html 签名?

问题描述

代码发布在这里并被报告可以工作,对我不起作用。

此代码抛出

运行时错误 '287 应用程序定义或对象定义错误

就行了Signature = Outmail.body

我想要当前 Outlook 用户的默认签名。

Private Sub Command33_Click()

Dim OutApp As Object
Dim OutMail As Object
Dim currentDate As Date
Dim DeliveryDate As String
Dim Recipients As String
Dim CarbonCopy As String
Dim Signature As Variant

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

currentDate = Format(Date, "dd/mm/yyyy")
Recipients = "a@gmail.com"
CarbonCopy = "b@gmail.com"
Signature = OutMail.Body

'msg hasn't been defined so it's commented out. msg in the body has been replaced with "msg".
'msg = "<span style='color:black'><p>Dear Team,</p>"

'msg = msg & "Thank you in advance</span>"

On Error Resume Next
With OutMail
    'Capture signature block.
    .Display
    Signature = .HTMLBody
    .To = Recipients
    .CC = CarbonCopy
    .Subject = "PSR " & currentDate
    .HTMLBody = "<span style = 'color:#1F497D'>" & "msg" & "</span>" & Signature
    .Attachments.Add ThisWorkbook.FullName
    .Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

End Sub

标签: vbams-accessoutlook

解决方案


如前所述,删除Signature = OutMail.Body.

然后在 之后放置一个断点Signature = .HTMLBody,并检查它的值。

这就是我在 Outlook 365 中所做的(它也适用于 Outlook 2010):

' Outlook HTML: after Mailitem.Display, .HtmlBody starts with
' <p class=MsoNormal><o:p>&nbsp;</o:p></p>
' Exactly in this line we want to put our HTML encoded mail text.
oItem.HtmlBody = Replace(oItem.HtmlBody, "&nbsp;", strMyBody, Count:=1)

默认签名在此行下方,并且保持不变。

编辑:our HTML encoded mail text意味着:我将纯文本传递给我的函数,并替换vbCrLf<br>.

如果您有实际的 HTML<p>等等,请改用Jeremy 的答案


推荐阅读