【问题标题】:Edit, send and save email to file system编辑、发送和保存电子邮件到文件系统
【发布时间】:2017-03-28 01:55:35
【问题描述】:

我们目前有一封由 Excel 使用 VBA 自动创建的电子邮件,主题、收件人、邮件正文以及模板文本均已填写。

Sub CreateMail(Optional sFile As String = "")
'Create email to send to requestor with attachment sFile

'Declarations
    Dim app As Outlook.Application
    Dim msg As Outlook.MailItem
    Dim send_to As Recipient
    Dim send_tos As Recipients

'Initiations
    Set app = CreateObject("Outlook.Application")
    Set msg = app.CreateItem(olMailItem)
    Set send_tos = msg.Recipients
    Set send_to = send_tos.Add("receiver@email.com")
    send_to.Type = 1

'Create message
    With msg
        .SentOnBehalfOfName = "sender@email.com"
        .Subject = "This is the email subject"
        .HTMLBody = "This is the email body" & vbCrLf
        'Resolve each Recipient's name.
            For Each send_to In msg.Recipients
              send_to.Resolve
            Next
        If Len(sFile) > 0 Then
            .Attachments.Add sFile
        End If
        .Display
    End With

End sub

在对创建的电子邮件进行一些手动更改后,我们希望将其发送并自动将副本保存到文件系统上的文件夹中(除了 Outlook 中通常发送的文件夹之外)。有没有办法在 Excel VBA 中完成这一切?

我怀疑使用 Outlook VBA 可能是可行的,但是文件夹是在 Excel 中定义的,我们希望将代码一起保存在一个文件中。

【问题讨论】:

  • 我们特别希望使用 Excel VBA 进行保存。
  • 如果您添加对 Microsoft Outlook 对象模型的引用,则 Excel VBA 可以使用其他答案中提到的方法。如果您不确定在哪里看,也许这会更好地作为答案?
  • 在链接解决方案中,电子邮件在 Outlook 中被选中,然后保存到文件夹中。而我是在从 Excel 创建的电子邮件触发发送事件后将电子邮件保存到文件夹之后。
  • 好的,没问题。那不是骗子

标签: vba excel


【解决方案1】:

您发送电子邮件的代码是什么?这适用于 Excel VBA 模块:

Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
    .BodyFormat = olFormatRichText
    .To = "email address"
    .Subject = "Test"
    .HTMLBody = "Test " & Now
    .DeleteAfterSubmit = True 'to not retain in sent folder
    .Display
    .SaveAs "C:\filepath\Test.txt", 0
'    .Send
End With

但是,猜测真正的技巧是允许在保存文件之前编辑电子邮件。到目前为止还没有看到解决方案。不幸的是,消息窗口打开时代码执行不会暂停。我希望暂停一下,因为 Office 应该是一个集成的应用程序套件——比如在 Access 中以对话模式打开一个表单,它会暂停代码的执行。

【讨论】:

    【解决方案2】:

    仅使用 Excel 中的代码,监控 SentItems 文件夹。

    Utilizing Outlook Events From Excel

    确认来自唯一 ID 的邮件。

    唯一 ID 可以在主题或正文中。

    您可以尝试将唯一 ID 保存在 PR_SEARCH_KEY 中。 How, can get the exact sent Email from Sent Items folder?How to uniquely identify an Outlook email as MailItem.EntryID changes when email is moved是同一个思路

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多