Outlook 2007 Tasks - VBA Customizations

by Andy Flesner

I've created a couple of custom VBA functions for Outlook 2007 from some snippets at www.outlookcode.com and thought that someone else may find them useful. The first allows you to create a task from an email and modify the Update List recipients (something that you cannot do manually). This also works in Outlook 2003. The second is a timestamp that allows you to inject a timestamp into a task body without modifying the format of the body (using the Word object library).


Create a Task from an Email with the Ability to add Update List Recipients


Instructions: Copy the code below and paste it into the VBA editor in Outlook (access the editor by pressing ALT+F11 in Outlook). Then create a button on the Quick Access Toolbar for mail messages that accesses the MakeTaskFromEmail() function.

Sub MakeTaskFromEmail()
   Dim strID As String
   Dim olNS As Outlook.NameSpace
   Dim olMail As Outlook.MailItem
   Dim objTask As Outlook.TaskItem
   Set objMail = Outlook.Application.ActiveInspector.CurrentItem

   strID = objMail.EntryID
   Set olNS = Application.GetNamespace("MAPI")
   Set olMail = olNS.GetItemFromID(strID)
   Set objFolder = olNS.PickFolder
   Set objTask = objFolder.Items.Add(olTaskItem)
   objRecipients = InputBox("Please enter any additional users to Update separated by semicolons:", "Update List Users")
    With objTask
      .Subject = olMail.Subject
      .Body = olMail.Body
      .StatusUpdateRecipients = olMail.SenderEmailAddress & "; " & objRecipients
      .StatusOnCompletionRecipients = olMail.SenderEmailAddress & "; " & objRecipients
   End With

   Call TaskAttachments(olMail, objTask)

   objTask.Display

   Set objTask = Nothing
   Set olMail = Nothing
   Set olNS = Nothing
End Sub

Sub TaskAttachments(objSourceItem, objTargetItem)
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set fldTemp = fso.GetSpecialFolder(2)
   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


TimeStamp Using WordEditor in Outlook 2007


Instructions: Copy the code below and paste it into the VBA editor in Outlook (access the editor by pressing ALT+F11 in Outlook). Then create a button on the Quick Access Toolbar for tasks that accesses the TimeStamp() function. You will also have to enable the Word object library in Tools -> References in the VBA editor.

Sub TimeStamp()
   Dim strText As String
   Dim objItem As Object
   Dim objExpl As Outlook.Explorer
   Dim objInsp As Outlook.Inspector
   Dim objDoc As Word.Document
   On Error Resume Next

   Set objNameSpace = Application.GetNamespace("MAPI")
   MyName = objNameSpace.CurrentUser.Name
   strText = ">> " & Date & " " & Time & " " & MyName & ":" & vbCrLf & vbCrLf & vbCrLf

   Set objExpl = Application.ActiveExplorer
   Set objItem = objExpl.Selection(1)
   If Not objItem Is Nothing Then
      Set objInsp = objItem.GetInspector
      If objInsp.EditorType = olEditorWord Then
         Set objDoc = objInsp.WordEditor
         objDoc.Characters(1).InsertBefore strText
      Else
         MsgBox "Cannot insert text in a formatted message unless Word is the editor"
      End If
   End If

   Set objInsp = Nothing
   Set objDoc = Nothing
   Set objExpl = Nothing
   Set objItem = Nothing
   Set objMsg = Nothing
End Sub

11 Comments

Anton
2008-02-05 17:40:19
I'm pretty technical, and got into the editor pasted the code, but from that point on the instruction is not "complete" ie. not sure how to get that command into the quick access toolbar. I can bring up the toolbar options, but don't know where to see the new command.


I use the "Gettings Things Done" add-on which takes care of creating a task from an email and more, but I would love to be able to mod the update list.

Andy Flesner
2008-02-05 19:08:40
Anton, it's a Macro. When customizing the toolbar, the MakeTaskFromEmail() function should be listed in the "Macros" category.


Andy

Jimmy Pena
2008-02-09 06:39:10
Thanks for posting this. You can make the MakeTaskFromEmail macro a bit more efficient by declaring objFolder as a MAPIFolder, and by using it to set an object reference to the default Tasks folder, which is where everyone usually keeps their tasks:


Set objFolder = olNS.GetDefaultFolder(olFolderTasks)



HTH,
JP

Andy Flesner
2008-02-09 13:54:50
Jimmy, originally that's how I had it set up. However, that wouldn't allow a user to specify a task folder in a different mailbox. The users I developed this for use multiple mailboxes, so I setup the folder selection for them.


Andy

Frank
2008-07-16 14:27:49
Hi thanks for posting, but I dont get this runnin in Outlook 2007 SP1 and all current patches.
If alwas ends at Set objMail = Outlook.Application.ActiveInspector.CurrentItem
Any Idea?
Andy Flesner
2008-07-16 15:17:51
@Frank - You must run the MakeTaskFromEmail() function from within an open email. If you are running it outside of an open message, then that would explain the object failure on the ActiveInspector's current item.
Frank
2008-07-18 08:19:49
@Andy
Thanks for coming back to me! much appreciatd.
Even from adding the Macro Button to the Quick Access Toolbar
I do get a csyntax error at
objRecipients = InputBox(”Please enter any additional users to Update separated by semicolons:”, “Update List Users”)


In edition, the one above and these two lines are red when pasting the code:


.StatusUpdateRecipients = olMail.SenderEmailAddress & “; ” & objRecipients
.StatusOnCompletionRecipients = olMail.SenderEmailAddress & “; ” & objRecipients


I am on a corpnet with exchange, could this be an issue?
However, other macros work.



Any hint is much appreciated.

Frank
2008-07-18 08:19:52
@Andy
Thanks for coming back to me! much appreciatd.
Even from adding the Macro Button to the Quick Access Toolbar
I do get a csyntax error at
objRecipients = InputBox(”Please enter any additional users to Update separated by semicolons:”, “Update List Users”)


In edition, the one above and these two lines are red when pasting the code:


.StatusUpdateRecipients = olMail.SenderEmailAddress & “; ” & objRecipients
.StatusOnCompletionRecipients = olMail.SenderEmailAddress & “; ” & objRecipients


I am on a corpnet with exchange, could this be an issue?
However, other macros work.



Any hint is much appreciated.

Frank
2008-07-18 08:19:55
@Andy
Thanks for coming back to me! much appreciatd.
Even from adding the Macro Button to the Quick Access Toolbar
I do get a csyntax error at
objRecipients = InputBox(”Please enter any additional users to Update separated by semicolons:”, “Update List Users”)


In edition, the one above and these two lines are red when pasting the code:


.StatusUpdateRecipients = olMail.SenderEmailAddress & “; ” & objRecipients
.StatusOnCompletionRecipients = olMail.SenderEmailAddress & “; ” & objRecipients


I am on a corpnet with exchange, could this be an issue?
However, other macros work.



Any hint is much appreciated.

Frank
2008-07-18 08:21:27
oops, sorry for tripple post, that wasnt the plan.
Andy Flesner
2008-07-18 10:45:20
Frank, try deleting and retyping the quotation marks after copying and pasting from this page. The pasted " marks are not being read properly by the editor.