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

时间:2015-11-30 11:58:22

标签: vba email outlook outlook-vba outlook-2013

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

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

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

1 个答案:

答案 0 :(得分:1)

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