Code level: beginner    Code area: Basic Outlook
Title: Create task from e-mail message (revised)
Description: This revised version of an earlier sample shows how to use the Namespace.GetItemFromID method in a "run a script" rule procedure to avoid getting security prompts in Outlook 2003 when the code accesses the Body property. Note that attachments cannot be copied directly between items. They must be saved to the system first, as discussed at
Date: 07-Jul-2005  01:03
Code level: beginner
Code area: Basic Outlook
Posted by: Sue Mosher
Sub MakeTaskFromMail2(MyMail As Outlook.MailItem)
    Dim strID As String
    Dim olNS As Outlook.NameSpace
    Dim olMail As Outlook.MailItem
    Dim objTask As Outlook.TaskItem
    strID = MyMail.EntryID
    Set olNS = Application.GetNamespace("MAPI")
    Set olMail = olNS.GetItemFromID(strID)
    Set objTask = Application.CreateItem(olTaskItem)
    With objTask
        .Subject = olMail.Subject
        .DueDate = olMail.SentOn
        .Body = olMail.Body
    End With
    Call CopyAttachments(olMail, objTask)
    Set objTask = Nothing
    Set olMail = Nothing
    Set olNS = 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

   Set fldTemp = Nothing
   Set fso = Nothing
End Sub
All 120comments
Page [ 1 2 3 4 5 6 7 8 9 10 Next >>  
  05-Aug-2005  07:42   
How do I get a toolbar button appear that can be clicked to run this code on the selected (or open) email?
  05-Aug-2005  15:09   
Set myItem = GetCurrentItem()
If myItem.Class = olMail Then
    MakeTaskFromMail2 myItem
End If

where GetCurrentItem is the function from the sample at
  23-Aug-2005  22:27   
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 -
  25-Aug-2005  08:30   
Brad, the code above will avoid a securitiy prompt in Outlook 2003. See for your options for other versions.
  14-Sep-2005  03:13   
Great script, question:

is there a way to add the original mail as an attachment in the body of the created task:
i know i can add the body of the mail, but i want add to the complete email as an attachment.

  14-Sep-2005  03:21   
Another question when adding the mail body text into the task body, i want to use the original format (Rich text or whatever), i used .bodyFormat etc but it doesnt work
original text is display as non formatted text

  14-Sep-2005  09:42   
Ernst, take a look at the Attachments.Add method. See

See for information on the issues involved and solutions available for working with formatted text.
  24-Sep-2005  19:07   
Can you specify what you did to stop the security prompt from appearing in 2003. I have my own routine that works fine as a macro. But when I run it as part of a rule I get the message that is mentioned above by Bradley.
  27-Sep-2005  08:16   
Greg, the key to avoiding prompts in Outlook 2003 VBA is to make sure every object derives from the intrinsic Application object. That's why the sample above uses the Namespace.GetItemFromID method to return the item that triggered the rule.
  15-Dec-2005  08:04   
If I wanted the .DueDate = olMail.SentOn + 1 day, do you know the code for this?

Page [ 1 2 3 4 5 6 7 8 9 10 Next >>