【问题标题】:VBA, Insert outlook signature in vba codeVBA,在 vba 代码中插入 Outlook 签名
【发布时间】:2018-03-25 17:13:46
【问题描述】:

我有一个 vba 代码,当到期日距当前日期至少 7 7 天时,它会自动发送电子邮件。

问题是他们在没有我的 Outlook 签名的情况下发送电子邮件。

代码是:

Sub email()
Dim lRow As Integer
Dim i As Integer
Dim toDate As Date
Dim toList As String
Dim eSubject As String
Dim eBody As String

With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .DisplayAlerts = False
End With

Sheets(1).Select
lRow = Cells(Rows.Count, 4).End(xlUp).Row

For i = 2 To lRow
toDate = Cells(i, 3)
 If toDate - Date <= 7 Then
     Set OutApp = CreateObject("Outlook.Application")
     Set OutMail = OutApp.CreateItem(0)

        toList = Cells(i, 4)    'gets the recipient from col D
        eSubject = "Doukementacion per  " & Cells(i, 2) & " Targa " & Cells(i, 5)
        eBody = "Pershendetje Adjona" & vbCrLf & vbCrLf & "Perfundo dokumentacionin e nevojshem per " & Cells(i, 2) & " me targa " & Cells(i, 5)

        On Error Resume Next
        With OutMail
        .To = toList
        .CC = ""
        .BCC = ""
        .Subject = eSubject
        .Body = eBody
        .bodyformat = 1
        '.Display   ' ********* Creates draft emails. Comment this out when you are ready
        .Send     '********** UN-comment this when you  are ready to go live
        End With
  On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
 Cells(i, 11) = "Mail Sent " & Date + Time 'Marks the row as "email sent in Column A"
End If
Next i

ActiveWorkbook.Save

With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .DisplayAlerts = True
End With
End Sub

【问题讨论】:

标签: excel vba outlook outlook-2016


【解决方案1】:

我发现将其设为HTMLBody 很有帮助。所以这部分:

With OutMail
    .To = toList
    .CC = ""
    .BCC = ""
    .Subject = eSubject
    .Body = eBody
    .bodyformat = 1
    '.Display   ' ********* Creates draft emails. Comment this out when you are ready
    .Send     '********** UN-comment this when you  are ready to go live
End With

看起来像

With OutMail
    .Display 'ads the signature
    .To = toList
    .Subject = eSubject
    .HTMLBody = eBody & .HTMLBody
    '.Display   ' ********* Creates draft emails. Comment this out when you are ready
    .Send     '********** UN-comment this when you  are ready to go live
    End With

您可能需要切换事件,不确定,因为我没有测试禁用事件

【讨论】:

  • 这是错误的 - 您不能连接 2 个 HTML 文档并生成有效的 HTML 文档 - 您的数据需要插入到 HTML 正文的适当位置。
  • 它有效,但可能是我使用它一年左右运气好,或者我在“身体”中遗漏了一些东西。从rondebruin.nl/win/s1/outlook/signature.htm中找到灵感
  • 它适用于该链接,因为附加的数据不是带有 html 和 head 标签的完整 HTML 文档。在您的情况下,您在有效的 HTML 文档开始之前附加数据。如果 Outlook 可以解析出来,那么您很幸运。一般来说,情况并非如此。
  • 你是对的。希望它有助于找到正确的解决方案
  • 正确的解决方案是将两者合并。请在stackoverflow.com/questions/8994116/…查看我的回答
【解决方案2】:

如果您的签名中没有图片并且可以使用.body,那么您可以使用我认为最简单的工具。

Sub Mail_Workbook_1()
    Dim OutApp As Object
    Dim Outmail As Object

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

    On Error Resume Next

    With OutMail
         .Display
    End With

         Signature = OutMail.body

    With OutMail

        .Subject = "This is the Subject line"
        .Body = strbody & Signature
        .Send    'or use .Display

    End with

    On Error GoTo 0

       Set Outmail = Nothing
       Set OutApp = Nothing

End Sub

祝你有美好的一天

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 2017-01-29
    • 2012-03-18
    • 1970-01-01
    • 2016-12-17
    • 2011-05-08
    相关资源
    最近更新 更多