Auto-conversion mail to task

Status
Not open for further replies.

pabe1250

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Hi!

I'm having kind of a challenge.

I'm trying to redirect received e-mails from a shared account in Outlook to a SharePoint task list. By doing so two steps are required:

1. Converting the received e-mail in the shared account's inbox to a task - which unfortunately, as I understand it, with no way of redirecting it, will end up in that same shared account's task list in Outlook.

2. Move the task from that account's task list to the SharePoint task list.

I found a Macro that converts the e-mail to a task and it works perfectly. And if I mark, drag and drop the task from the Outlook default task list in to the SharePoint list it works just fine, but when I try to use a Macro doing this it just doesn't work. If I put up a rule telling that all received e-mails in this account should be moved to that SharePoint list and then run the Macro to convert it to a task, it just moves the e-mail. The conversion never takes place. Though if I just run the conversion Macro it really works. The e-mail becomes a task - but unfortunately placed in the wrong directory.

It seems as if the Macro can't be run after a move on the e-mail has occured.

Any work-arounds? Preferably a way to do this on the local machine since involving other departments further up the ladder will just add to my hardship:D

Thanks in advance!

/Paul
 

pabe1250

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
This is the Macro:

Sub ConvertMailtoTask(Item As Outlook.MailItem)
Dim objTask As Outlook.TaskItem
Set objTask = Application.CreateItem(olTaskItem)

With objTask
.Subject = Item.Subject
.StartDate = Item.ReceivedTime
.Body = Item.Body
.Save

End With
Set objTask = Nothing
Set SPSFolder = GetFolderPath("SharePoint Lists\Any_given_subfolder")


Set objTask = Application.CreateItem(olTaskItem)


objTask.Save


objTask.Move SPSFolder

End Sub

When run I get a compilation error "Sub or Function has not been defined" and it jumps to "GetFolderPath" which turns blue. Any ideas?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
You need the GetFolderPath function - Working with VBA and non-default Outlook Folders - Slipstick Systems

Not tested, but this should work -

Sub ConvertMailtoTask(Item As Outlook.MailItem)
Dim objTask As Outlook.TaskItem
Set objTask = Application.CreateItem(olTaskItem)

'move this up here just to get it out of the way

Set SPSFolder = GetFolderPath("SharePoint Lists\Any_given_subfolder")

' this creates and saves the task
With objTask
.Subject = Item.Subject
.StartDate = Item.ReceivedTime
.Body = Item.Body
.Save
End With

' this can go right before 'end with' (if so remove ojbtask from it)
objTask.Move SPSFolder

' this goes last, before the sub ends

Set objTask = Nothing

End Sub
 

pabe1250

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
Thanks Diane! It really helps me:rolleyes:

So the GetFolderPath function goes before the code you wrote?


Is it enough to state: Set Items = GetFolderPath("SharePoint Lists\Any_given_subfolder").Items

....is the function included in the system or is it needed to include the whole code - like this?

Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder
Dim oFolder As Outlook.Folder

Dim FoldersArray As Variant

Dim i As Integer


On Error GoTo GetFolderPath_Error

If Left(FolderPath, 2) = "\\" Then

FolderPath = Right(FolderPath, Len(FolderPath) - 2)

End If

'Convert folderpath to array

FoldersArray = Split(FolderPath, "\")

Set oFolder = Application.Session.Folders.item(FoldersArray(0))

If Not oFolder Is Nothing Then

For i = 1 To UBound(FoldersArray, 1)

Dim SubFolders As Outlook.Folders

Set SubFolders = oFolder.Folders

Set oFolder = SubFolders.item(FoldersArray(i))

If oFolder Is Nothing Then

Set GetFolderPath = Nothing

End If

Next

End If

'Return the oFolder

Set GetFolderPath = oFolder

Exit Function


GetFolderPath_Error:

Set GetFolderPath = Nothing

Exit Function

End Function

/Paul
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
So the GetFolderPath function goes before the code you wrote?

Is it enough to state: Set Items = GetFolderPath("SharePoint Lists\Any_given_subfolder").Items

> ...is the function included in the system or is it needed to include the whole code - like this?
I always put it after - actually, I have a module called 'functions' where this function and a function that works on open or selected items are saved so I can use them in any macro - but if you are putting it in the same module, put it after your macro.

You need the whole code - call it in your macro as Set SPSFolder = GetFolderPath("SharePoint Lists\Any_given_subfolder"), just like in the code sample posted earlier.
 

pabe1250

Member
Outlook version
Outlook 2007
Email Account
Exchange Server
I always put it after - actually, I have a module called 'functions' where this function and a function that works on open or selected items are saved so I can use them in any macro - but if you are putting it in the same module, put it after your macro.

You need the whole code - call it in your macro as Set SPSFolder = GetFolderPath("SharePoint Lists\Any_given_subfolder"), just like in the code sample posted earlier.
Thanks again. It finally worked like a charm:)

Did a silly mistake though since my system is in swedish where "SharePoint Lists" is "SharePoint-listor". Therefore the path was screwed up and eventhough the macro ran without any faults, the actual move of the task went to a non-existing folder.

Tack så mycket:)
 
Status
Not open for further replies.
Top