excel - 向自动 Outlook 邮件添加签名

标签 excel vba outlook

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

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

最佳答案

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

上面的代码中没有声明您的“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

关于excel - 向自动 Outlook 邮件添加签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51381449/

相关文章:

html - 如何在电子邮件中嵌入图像?

vba - IMAP 文件夹上的 Outlook ItemAdd 事件仅在选择文件夹时触发

java - 使用 Apache Poi 读取内部存储中的 excel xlsx 文件

python - 使用 SMTP 和 Python 3 发送 xlsx 文件

ruby-on-rails - 如何在 ruby​​/rails 中编辑 .xlsm 文件而不丢失 VBA 宏?

excel - 为一系列列运行相同的 VBA 代码(一次一列)

excel - 在工作表上进行更改时使 VBA 函数自动更新 - excel

html - Outlook 2007 Html 表格 td 元素

vba - Worksheet_change 宏在 Excel 2007 中多次运行

vba - 将 Excel 工作表复制到另一本 Excel 书中,但仅复制格式和值