VBA 发送 Outlook 电子邮件并将其附加到任务

VBA Send Outlook Email and attach it to Task

我有一个创建电子邮件和任务的循环。我需要找到自动发送电子邮件并将该电子邮件附加到创建的任务,保存并关闭。

下面是非常简化的代码,用于创建电子邮件和任务并将它们保留在显示模式中,以便手动发送和附加到任务中。

Dim olApp As Object
Dim ns As Object
Dim oltask As Object
Dim SharedFolder As Object
Dim MyApp As Object

Set MyApp = CreateObject("Outlook.Application")
for i = 0 to 3
    If IsObject(MyApp) Then
        Set MyItem = MyApp.CreateItem(0) 'olMailItem
        With MyItem
            .SentOnBehalfOfName = "Email@email.com"
            .Bcc = Contact
            .Subject = "SUBJECT"
            .ReadReceiptRequested = False
            .HTMLBody = Email_body
            .Attachments.Add attachement_pdf
            .Display
        End With
        'MyItem.Send 'to send an email

        Set ns = MyApp.GetNamespace("MAPI")
        ns.logon
        Set Recip = ns.CreateRecipient("Inboxname")
        Set SharedFolder = ns.GetSharedDefaultFolder(Recip, 13)
        Set oltask = SharedFolder.Items.Add("IPM.Task")

        With oltask
           .Subject = "SUBJECT"
           .StartDate = Date
           .DueDate = Date + 7
           .Status = 1
           .Importance = 1
           .ReminderSet = False
           .body = task_body
           .Display
           .Attachments.Add 'HOW TO ADD SENT EMAIL?
        End With
        'oltask.Save 'save the task
    End if
Next i

P.S。展望 2013

不幸的是,这并不像您希望的那样简单。您必须等到电子邮件发送后才能将其作为对象添加到 TaskItem.Attachments.Add 方法的 Source 参数中。这意味着您必须先发送电子邮件,然后监控已发送邮件文件夹的 Folder.Items.ItemAdd 事件。 ItemAdd 事件的 Item 参数将使您能够访问在传递后刚刚添加到该文件夹​​的 MailItem。