简体   繁体   中英

VBA Send Outlook Email and attach it to Task

I have a loop where I create email and a task. I need to find away to automatically send email and attach that email to created task, save and close.

Below is very simplified code to create email and task and leave them in display mode left for manual sending and attaching in the 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

PS Outlook 2013

Unfortunately this isn't as straightforward as you'd like. You have to wait until the email is sent before you can add it as an object to the Source parameter of the TaskItem.Attachments.Add method. Which means you'll have to send your emails first, then monitor the Folder.Items.ItemAdd event for the Sent Items folder. The Item parameter of the ItemAdd event will give you access to the MailItem that was just added to that folder after delivery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM