向自动Outlook邮件添加签名

时间:2018-07-17 12:30:26

标签: excel vba excel-vba outlook outlook-vba

我正在尝试在发送的自动邮件的末尾添加签名。我希望签名是运行宏的用户的默认签名。我编写的代码运行时不会崩溃,但不会插入签名。我在下面提供代码。

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 String

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 = "<span style='color:black'><p>Dear Team,</p>"

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

On Error Resume Next
With OutMail
    .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

3 个答案:

答案 0 :(得分:3)

必须将签名声明为变体,并且必须首先显示空电子邮件以捕获它。

您的“ msg”未在上述代码中声明。我假设您已经覆盖了。否则,您的代码将无法正常工作。鉴于此假设...

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

答案 1 :(得分:1)

首先,您无法连接两个HTML字符串,并且不能返回有效的HTML字符串。它们必须合并。不仅如此,您还需要合并两个HTML文档中的样式等。

第二,要检索您的情况的签名,必须首先显示MailItem-调用Display,然后再读取HTMLBody属性。

如果使用Redemption是一个选项,它将公开RDOSignature对象,并允许插入任何签名,而无需使用RDOSignature.ApplyTo()方法显示消息。

答案 2 :(得分:0)

我认为您的mailItem未激活。在尝试获取正文之前,请尝试添加OutMail.display。然后它应该起作用。