More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 23-Mar-2017 00:56

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: beginner    Code area: Basic Outlook Printer Friendly Version
Title: Make task from mail message
Description: The MakeTaskFromMail procedure is designed to be invoked by the "run a script" rule action in the Outlook 2002/3 Rules Wizard. Note that attachments cannot be copied directly. They must be saved to the system first, as discussed at http://www.outlookcode.com/d/code/copyatts.htm
Date: 29-Nov-2004  09:07
Code level: beginner
Code area: Basic Outlook
Posted by: Sue Mosher
This message is displayed as VB.NET
 Sub MakeTaskFromMail(MyMail As Outlook.MailItem)
    Dim objTask As Outlook.TaskItem
    Set objTask = Application.CreateItem(olTaskItem)
    With objTask
        .Subject = MyMail.Subject
        .DueDate = MyMail.SentOn
        .Body = MyMail.Body
    End With
    Call CopyAttachments(MyMail, objTask)
    objTask.Save
    Set objTask = Nothing
End Sub

Sub CopyAttachments(objSourceItem, objTargetItem)
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set fldTemp = fso.GetSpecialFolder(2) ' TemporaryFolder
   strPath = fldTemp.Path & "\"
   For Each objAtt In objSourceItem.Attachments
      strFile = strPath & objAtt.FileName
      objAtt.SaveAsFile strFile
      objTargetItem.Attachments.Add strFile, , , objAtt.DisplayName
      fso.DeleteFile strFile
   Next

   Set fldTemp = Nothing
   Set fso = Nothing
End Sub
All 21comments
Page [ 1 2 3 Next >>  
  30-Nov-2004  23:23   
I've been looking for some code to create a task from an email using a rule, so this really was timely. Below is the current code I'm using. The last thing I'd like to do is set the body of the Task to the body of the email, but I'd like to include all the normal headers in the task body too. Manually I would select the email, click Forward, copy the body, and paste it into the task body (or I would right-click drag the mail to the Task folder and copy as text).

Is there a way to use the forward method with redemption to do this? Below is the current code.

Public Sub WaitingForFromEmail(MyMail As Outlook.MailItem)
  ' Comments :
  ' Parameters: -
  ' Modified :
  '
  ' --------------------------------------------------
  Dim objTask As Outlook.TaskItem
  Dim objApp As Outlook.Application
  'Dim objCurrentItem As Object
  Dim objRecips As SafeRecipients
  Dim objRecip As SafeRecipient
  Dim objWFMail
  
  Set objApp = Outlook.CreateObject("Outlook.Application")
  Set objWFMail = CreateObject("Redemption.SafeMailItem")
  objWFMail.Item = MyMail
  If objWFMail.Class = olMail Then
    Set objRecips = objWFMail.Recipients
    For Each objRecip In objWFMail.Recipients
      If objRecip.Type = olTo Then
       Set objTask = objApp.CreateItem(olTaskItem)
       objTask.Body = objWFMail.Body
       objTask.Subject = objRecip.Name & " - " & Format(objWFMail.SentOn, "mm/dd/yyyy") & " - " & objWFMail.Subject
       objTask.Categories = "@Waiting For"
       objTask.Save
      End If
    Next
  End If
End Sub

Thanks,
Brian
  02-Dec-2004  08:54   
I don't see that you're forwarding anything. Wouldn't you want to use:

    Set objForward = MyMail.Forward
    objWFMail.Item = objForward

and then use objWFMail.Body?
  03-Dec-2004  15:57   
Great, thanks! I actually created a new Redemption item to assign the .Forward to, since I didn't want to lose the Recipients property on the objWFMail.

Here's the revised code:

Public Sub WaitingForFromEmail(MyMail As Outlook.MailItem)
  ' Comments :
  ' Parameters: -
  ' Modified :
  '
  ' --------------------------------------------------
  Dim objTask As Outlook.TaskItem
  Dim objApp As Outlook.Application
  'Dim objCurrentItem As Object
  Dim objRecips As SafeRecipients
  Dim objRecip As SafeRecipient
  Dim objWFMail
  Dim objForward
  
  Set objApp = Outlook.CreateObject("Outlook.Application")
  Set objWFMail = CreateObject("Redemption.SafeMailItem")
  objWFMail.Item = MyMail
  Set objForward = CreateObject("Redemption.SafeMailItem")
  objForward.Item = MyMail.Forward
  
  If objWFMail.Class = olMail Then
    Set objRecips = objWFMail.Recipients
    For Each objRecip In objWFMail.Recipients
      If objRecip.Type = olTo Then
       Set objTask = objApp.CreateItem(olTaskItem)
       objTask.Body = objForward.Item.Body
       objTask.Subject = objRecip.Name & " - " & Format(objWFMail.SentOn, "mm/dd/yyyy") & " - " & objWFMail.Subject
       objTask.Categories = "@Waiting For"
       objTask.Save
      End If
    Next
  End If
End Sub
  03-Dec-2004  16:33   
Actually this doesn't work. I get prompted for Security when assigning the olTask.body...
  06-Dec-2004  11:33   
Never mind, I think it was because I was 'watching' something in the VBA watch list...

Here's the final code:

Public Sub WaitingForFromEmail(MyMail As Outlook.MailItem)
  ' Comments :
  ' Parameters: -
  ' Modified : 12/06/2004 - B.Davidson
  '
  ' --------------------------------------------------
  Dim objTask As Outlook.TaskItem
  Dim objApp As Outlook.Application
  Dim objRecips As SafeRecipients 'All recipients on message
  Dim objRecip As SafeRecipient 'Current recipient
  Dim objWFMail 'for Redemption mail item
  Dim objForward 'for Redemption mail item for getting mail body with headers
  
  
  Set objApp = Outlook.CreateObject("Outlook.Application")
  
  Set objWFMail = CreateObject("Redemption.SafeMailItem") 'use Redemption to bypass the security prompts
  objWFMail.Item = MyMail 'use Redemption to bypass the security prompts
  
  If objWFMail.Class = olMail Then 'only run if the current item is a mail message
    
    Set objRecips = objWFMail.Recipients 'get all the recipients on the message (To, CC)
    
    For Each objRecip In objRecips 'check all the recipients
      
      If objRecip.Type = olTo Then 'if the current recipient is on the To line, create a @WF task
      
        Set objTask = objApp.CreateItem(olTaskItem) 'create a blank task
        
        'Set the subject per the DA white paper format (Name, Date, Subject
        objTask.Subject = objRecip.Name & " - " & Format(objWFMail.SentOn, "mm/dd/yyyy") & " - " & objWFMail.Subject
        
        'Create a forwarded copy of the message to preserve the headers in the @WF task
        Set objForward = CreateObject("Redemption.SafeMailItem")
        objForward.Item = MyMail.Forward
        objForward.Save 'save the draft forward message so redemption can see it
        
        objTask.Body = objForward.Body 'copy the email body with basic headers to task note field
        
        objForward.Delete 'delete the draft message (no longer needed)
        
        objTask.Categories = "@Waiting For" 'Change to whatever category you like
        objTask.Save 'save your work!
      
      End If 'objRecip.Type = olTo
    
    Next 'go process the next recipient
  Else:
    
    Exit Sub
  End If
  
  'Variable cleanup
  Set objTask = Nothing
  Set objForward = Nothing
  Set objRecip = Nothing
  Set objRecips = Nothing
  Set objWFMail = Nothing
  Set objApp = Nothing
  
End Sub
  07-Jul-2005  09:25   
See http://www.outlookcode.com/codedetail.aspx?id=959 for a revised version of this sample that will avoid security prompts in Outlook 2003 without the need to use Redemption.
  23-Aug-2005  22:24   
Hi hows it going!

I've been testing this script, and it works well, however Outlook comes up each time saying: A Program is trying to access e-mail addresses you have stored in Outlook. Do you want to Continue? If this is unexpected, it may be a virus and you should choose "NO""

And then has an option to only "Allow Access for (dropdown box) list of 1,2,5,10,15mins" - is there anyway to indefinately allow access?

The Rule is based on a Distribution List stored on our Address Books -
  24-Aug-2005  08:50   
Bradley, for Outlook 2003, use this revised version:

Sub MakeTaskFromMail(MyMail As Outlook.MailItem)
    Dim strID as String
    Dim objMail as Outlook.MailItem
    Dim objTask As Outlook.TaskItem
    Dim objNS as Outlook.Namespace
   
    Set objNS = Application.GetNamespace("MAPI")
    strID = MyMail.EntryID
    Set objMail = objNS.GetItemFromID(strID)

    Set objTask = Application.CreateItem(olTaskItem)
    With objTask
        .Subject = objMail.Subject
        .DueDate = objMail.SentOn
        .Body = objMail.Body
    End With
    Call CopyAttachments(objMail, objTask)
    objTask.Save
    Set objTask = Nothing
    Set objMail = Nothing
End Sub

For Outlook 2002 SP3, see http://www.outlookcode.com/d/sec.htm for your options.
  24-Aug-2005  17:55   
Hi Sue,

It seems though, whenever I re-create this rule, or run it - Outlook generates an error and that "Microsoft Office Outlook has encourntered a problem and needs to close. We are sorry...." and then it goes on about Error Reporting and just restarts Outlook.

Do you have an e-mail that I can contact you on? Mybe I could send you screenshots of what i'm doing, i'd really appreciate the help.
  25-Aug-2005  08:26   
Brad, that's interesting! I don't get an error here under OL2003 SP1. What condition(s) are you using for the rule?
Page [ 1 2 3 Next >>