# Outlook - convert an email into a task



## alexondrums (Apr 1, 2007)

I’ve found a very handy VB Macro that I'm using in Outlook (2002), that converts an email into a task, and sets various parameters – 


```
Public Sub CreateTaskFromItem()
Dim olTask As Outlook.TaskItem

'Using object rather than MailItem, so that it
'can handle posts, meeting requests, etc as well

Dim olItem As Object
Dim olExp As Outlook.Explorer
Dim fldCurrent As Outlook.MAPIFolder
Dim olApp As Outlook.Application

Set olApp = Outlook.CreateObject("Outlook.Application")
Set olTask = olApp.CreateItem(olTaskItem)
Set olExp = olApp.ActiveExplorer
Set fldCurrent = olExp.CurrentFolder

Dim cntSelection As Integer
cntSelection = olExp.Selection.Count

For i = 1 To cntSelection
Set olItem = olExp.Selection.Item(i)
olTask.Attachments.Add olItem
olTask.Subject = "Follow up on " & olItem.Subject
'olItem.Delete 

Next

olTask.Display
'Set the due date for today
olTask.DueDate = DateAdd("h", 48, Now)

'Set the reminder for 24 hours from now
olTask.ReminderSet = True
olTask.ReminderTime = DateAdd("h", 24, Now)

'Saving the task item, so that in case I close it, I won't lose
'the items which were deleted after being attached to the task
'olTask.Save

End Sub
```
I've commented out the command to delete the email from the inbox and to save it to tasks as I want to perform this step manually. I've also adjusted the due and reminder parameters to suit me.

It all works fine, however I would like the subject field to contain:

*Sender Name - Subject*

I take it that the line…


```
olTask.Subject = "Follow up on " & olItem.Subject
```
 …where olItem.Subject adds the email subject after the text string “Follow up on”

Using this syntax, I’ve tried changing the word Subject to From and FullName to test to include this in the subject, however this doesn’t work. I figured it should look like this…


```
olTask.Subject = olItem.From & "-" & olItem.Subject
```
 …to get what I want, but ‘From’ doesn’t appear to be the correct property.

Can anyone help?


----------



## yoyoma (Jan 12, 2009)

Thanks for sharing your VBA. I wanted to do a similar thing and used your code as a stepping stone to the following. 

My code copies an email message as an attachment, pastes it into a new task, gives task a subject like email with time stamp and brackets so you can see that this is a "email Converted" task. It marks the email with a yellow flag so you know you processed it. It copies the body text of the email, and pastes that into the task also. If you have more that one email selected, it grabs all of them, and puts them into ONE task (doing all the things mentioned above also).

I made the code work for a stand alone outlook folder, so it does not put all this into your default task folder. I had to do this because, my work Exchange Outlook account has size limits that I am always exceeding with all the email I get, so I have to have the separate file, that is saved on my network. I wrote the code so that it was simple to understand this. That is where the ToDo references are. I think the ToDo references can be replaced with the default location of the task folder, and then people can have the tasks where they normally are.

Here is the code:

Public Sub Email_to_Task()

On Error GoTo eexit
Dim strSubject As String
Dim msg As Variant
Let strSubject = ""
Dim strBody As Variant
Dim olTask As Outlook.TaskItem

'Using object rather than MailItem, so that it
'can handle posts, meeting requests, etc as well

Dim olItem As Object
Dim olExp As Outlook.Explorer
Dim fldTask As Outlook.MAPIFolder
Dim olApp As Outlook.Application

Set olApp = Outlook.CreateObject("Outlook.Application")
Set olTask = Application.Session.Folders.Item("todos").Folders.Item("todos").Items.Add
Set olExp = olApp.ActiveExplorer
Set fldTask = Application.Session.Folders.Item("todos").Folders.Item("todos")
'Set fldCurrent = olExp.CurrentFolder
'Set fldCurrent = olExp.SelectFolder("test"

Dim cntSelection As Integer
cntSelection = olExp.Selection.Count

For i = 1 To cntSelection
Set olItem = olExp.Selection.Item(i)
olItem.FlagIcon = olYellowFlagIcon
olItem.Save
Let strSubject = strSubject & "[" & olItem.SenderName & ": " & olItem.Subject & "(" & olItem.SentOn & ")] "
Let strBody = strBody & vbNewLine & "*** EMAIL " & i & " w/" & olItem.Attachments.Count & " attmts: " & vbNewLine & olItem.Body
'olItem.Save
'olItem.Attachments.Count
'MsgBox "there are " & olItem.Attachments.Count & " attachments in the email"
olTask.Attachments.Add olItem

'MsgBox olItem.SenderName & " " & olItem.SentOn

'olItem.Delete

Next
olTask.Subject = strSubject
olTask.Body = strBody
olTask.Display
'Set the due date for today
'-olTask.DueDate = DateAdd("h", 48, Now)

'Set the reminder for 24 hours from now
'-olTask.ReminderSet = True
'-olTask.ReminderTime = DateAdd("h", 24, Now)

'Saving the task item, so that in case I close it, I won't lose
'the items which were deleted after being attached to the task
'
Let msg = MsgBox("Save & close task now?", vbYesNo, "Email To Task Converter")

If msg = vbYes Then
olTask.Close (olSave)
End If
eexit:
End Sub


----------



## yoyoma (Jan 12, 2009)

Thanks for sharing your VBA. I wanted to do a similar thing and used your code as a stepping stone to the following. 

My code copies an email message as an attachment, pastes it into a new task, gives task a subject like email with time stamp and brackets so you can see that this is a "email Converted" task. It marks the email with a yellow flag so you know you processed it. It copies the body text of the email, and pastes that into the task also. If you have more that one email selected, it grabs all of them, and puts them into ONE task (doing all the things mentioned above also).

I made the code work for a stand alone outlook folder, so it does not put all this into your default task folder. I had to do this because, my work Exchange Outlook account has size limits that I am always exceeding with all the email I get, so I have to have the separate file, that is saved on my network. I wrote the code so that it was simple to understand this. That is where the ToDo references are. I think the ToDo references can be replaced with the default location of the task folder, and then people can have the tasks where they normally are.

Here is the code:

Public Sub Email_to_Task()

On Error GoTo eexit
Dim strSubject As String
Dim msg As Variant
Let strSubject = ""
Dim strBody As Variant
Dim olTask As Outlook.TaskItem

'Using object rather than MailItem, so that it
'can handle posts, meeting requests, etc as well

Dim olItem As Object
Dim olExp As Outlook.Explorer
Dim fldTask As Outlook.MAPIFolder
Dim olApp As Outlook.Application

Set olApp = Outlook.CreateObject("Outlook.Application")
Set olTask = Application.Session.Folders.Item("todos").Folders.Item("todos").Items.Add
Set olExp = olApp.ActiveExplorer
Set fldTask = Application.Session.Folders.Item("todos").Folders.Item("todos")
'Set fldCurrent = olExp.CurrentFolder
'Set fldCurrent = olExp.SelectFolder("test"

Dim cntSelection As Integer
cntSelection = olExp.Selection.Count

For i = 1 To cntSelection
Set olItem = olExp.Selection.Item(i)
olItem.FlagIcon = olYellowFlagIcon
olItem.Save
Let strSubject = strSubject & "[" & olItem.SenderName & ": " & olItem.Subject & "(" & olItem.SentOn & ")] "
Let strBody = strBody & vbNewLine & "*** EMAIL " & i & " w/" & olItem.Attachments.Count & " attmts: " & vbNewLine & olItem.Body
'olItem.Save
'olItem.Attachments.Count
'MsgBox "there are " & olItem.Attachments.Count & " attachments in the email"
olTask.Attachments.Add olItem

'MsgBox olItem.SenderName & " " & olItem.SentOn

'olItem.Delete

Next
olTask.Subject = strSubject
olTask.Body = strBody
olTask.Display
'Set the due date for today
'-olTask.DueDate = DateAdd("h", 48, Now)

'Set the reminder for 24 hours from now
'-olTask.ReminderSet = True
'-olTask.ReminderTime = DateAdd("h", 24, Now)

'Saving the task item, so that in case I close it, I won't lose
'the items which were deleted after being attached to the task
'
Let msg = MsgBox("Save & close task now?", vbYesNo, "Email To Task Converter")

If msg = vbYes Then
olTask.Close (olSave)
End If
eexit:
End Sub


----------



## guiness_joe (Aug 8, 2009)

Can you please advise how to save the task in a personal folder?

The folder name is Personal folder/Task


----------



## yoyoma (Jan 12, 2009)

guiness_joe,
Thanks for the question. I updated the VBA code since I originally posted. The revised code is below.
The code is updated to address your issuse of being able to put new tasks into the default outlook folder named "Personal Folders". If your folder is actually named "Personal Folder" (without the s at the end of the word Folder) then just remove the s from the name and it should work.

NOTICE FOR EVERYONE:
To save your new "Email-To-Task" task to any folder in outlook just replace the name "Personal Folder" with the name of the folder you want the task to be saved in.


```
Public Sub Email_to_Task()

On Error GoTo eexit
Dim strSubject As String
Dim msg As Variant
Let strSubject = ""
Dim strBody As Variant
Dim olTask As Outlook.TaskItem
Dim strPreSubject As String

'Using object rather than MailItem, so that it
'can handle posts, meeting requests, etc as well

Dim olItem As Object
Dim olExp As Outlook.Explorer
Dim fldTask As Outlook.MAPIFolder
Dim olApp As Outlook.Application

Set olApp = Outlook.CreateObject("Outlook.Application")
Set olTask = Application.Session.Folders.Item("Personal Folders").Folders.Item("Tasks").Items.Add


Set olExp = olApp.ActiveExplorer
Set fldTask = Application.Session.Folders.Item("Personal Folders").Folders.Item("Tasks")


Dim cntSelection As Integer
cntSelection = olExp.Selection.Count

For i = 1 To cntSelection
Set olItem = olExp.Selection.Item(i)
olItem.FlagIcon = olYellowFlagIcon
olItem.Save
Let strSubject = strSubject & "[" & olItem.Subject & "(" & olItem.SenderName & "-" & olItem.SentOn & ")]     "
Let strBody = strBody & vbNewLine & "*** EMAIL " & i & " w/" & olItem.Attachments.Count & " attmts: " & vbNewLine & olItem.Body
olTask.Attachments.Add olItem


Next
Let strPreSubject = InputBox("Enter a subject for the new task you are creating and press OK, or press CANCEL to just accept the default subject.", "Email To Task Converter")
olTask.Subject = strPreSubject & " " & strSubject
olTask.Body = strBody
olTask.StartDate = Now
olTask.Display
olTask.Save


End
eexit:
MsgBox "A process was canceled so the task may not have been created.", vbOKOnly, "Email To Task Converter"
olItem.FlagIcon = olNoFlagIcon
olItem.Save
End Sub
```


----------



## guiness_joe (Aug 8, 2009)

Alexondrums, Thank you for your help!

The attached email is pasted at the end of the task body, can it be pasted at the beginning of the task body?


----------



## yoyoma (Jan 12, 2009)

To attach the email at the beginning of the task body you would have to insert code after the FOR NEXT loop that puts the curser/record marker at the end of the task. Other than that you would have to totally reconstruct the code. 

Another option is to comment out the text within the task all together, just leaving the attachments.

Good Luck!


----------



## guiness_joe (Aug 8, 2009)

Thank you so much!


----------

