Last active
March 17, 2018 23:46
-
-
Save zzamboni/868510 to your computer and use it in GitHub Desktop.
Visual Basic code for creating tasks from email messages in Outlook. See full description at http://zzamboni.org/post/automatically-creating-tasks-from-email-in-outlook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Make a task from a message, processing the subject to look for contexts like @home or @office, if | |
' any are found the new task is assigned to the matching category. | |
' For example if the subject contains "@of" and the @OFFICE category exists, it will be used. | |
' If the subject starts with "todo:" it will be removed before creating the task. | |
' Diego Zamboni, April 5th, 2011 | |
' Set the string comparison method to Text ("AAA" = "aaa"). | |
Option Compare Text | |
Sub MakeTaskWithAttachmentFromMessageMacro() | |
Dim curMail As Outlook.MailItem | |
Set curMail = GetCurrentItem() | |
Call MakeTaskWithAttachmentFromMessage(curMail) | |
End Sub | |
' Generic function to make a task from an email message. Scans the subject line | |
' for contexts of the form @context and if found, assign it to that category | |
Sub MakeTaskWithAttachmentFromMessage(MyMail As Outlook.MailItem) | |
Dim strID As String | |
Dim olNS As Outlook.NameSpace | |
Dim olMail As Outlook.MailItem | |
Dim objTask As Outlook.TaskItem | |
Dim cats, cat | |
Dim regex, match, matches | |
Dim taskcat | |
Dim catstrs As New Collection | |
Dim s | |
Dim newsubject As String | |
strID = MyMail.EntryID | |
Set olNS = Application.GetNamespace("MAPI") | |
Set olMail = olNS.GetItemFromID(strID) | |
Set objTask = Application.CreateItem(olTaskItem) | |
objTask.Attachments.Add MyMail | |
Set cats = olNS.categories | |
For Each cat In cats | |
catstrs.Add ("" & cat) | |
s = s & "; " & cat | |
Next | |
'MsgBox ("Categories: " & s) | |
Set regex = CreateObject("vbscript.regexp") | |
regex.Pattern = " (@[^ ]*)" | |
regex.IgnoreCase = True | |
regex.Global = True | |
Set matches = regex.Execute(olMail.subject) | |
taskcat = "" | |
newsubject = olMail.subject | |
newsubject = Replace(newsubject, "todo:", "") | |
For Each match In matches | |
matchstr = "" & match.submatches(0) & "*" | |
newsubject = Replace(newsubject, match, "") | |
For Each s In catstrs | |
If s Like matchstr Then | |
taskcat = s | |
End If | |
Next | |
Next | |
'MsgBox ("taskcat = " & taskcat) | |
With objTask | |
.subject = Trim(newsubject) | |
.Body = olMail.Body | |
.categories = taskcat | |
End With | |
objTask.Save | |
Set objTask = Nothing | |
Set olMail = Nothing | |
Set olNS = Nothing | |
End Sub | |
' From http://www.outlookcode.com/codedetail.aspx?id=50 | |
Function GetCurrentItem() As Object | |
Dim objApp As Outlook.Application | |
Set objApp = CreateObject("Outlook.Application") | |
On Error Resume Next | |
Select Case TypeName(objApp.ActiveWindow) | |
Case "Explorer" | |
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1) | |
Case "Inspector" | |
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem | |
Case Else | |
' anything else will result in an error, which is | |
' why we have the error handler above | |
End Select | |
Set objApp = Nothing | |
End Function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' How to use it: see http://zzamboni.org/post/automatically-creating-tasks-from-email-in-outlook | |
' Diego Zamboni, March 16, 2011 | |
' Set the string comparison method to Text ("AAA" = "aaa"). | |
Option Compare Text | |
' Based originally on code from http://www.outlookcode.com/codedetail.aspx?id=959, modified by Diego Zamboni | |
Sub MakeWaitingForTaskWithAttachmentFromCurrentMessage(MyMail As Outlook.MailItem) | |
Dim strID As String | |
Dim olNS As Outlook.NameSpace | |
Dim olMail As Outlook.MailItem | |
Dim objTask As Outlook.TaskItem | |
Dim categories As String | |
Dim addRecipient As Boolean | |
Dim regex | |
Dim matches, customSubject, subject | |
' Configuration options | |
categories = "@WAITING FOR" | |
addRecipient = True | |
strID = MyMail.EntryID | |
Set olNS = Application.GetNamespace("MAPI") | |
Set olMail = olNS.GetItemFromID(strID) | |
Set objTask = Application.CreateItem(olTaskItem) | |
objTask.Attachments.Add MyMail | |
Set regex = CreateObject("vbscript.regexp") | |
regex.Pattern = "/wf (.*)" | |
regex.IgnoreCase = True | |
regex.Global = True | |
Set matches = regex.Execute(olMail.Body) | |
If matches.Count <> 0 Then | |
customSubject = matches(0).submatches(0) | |
Else | |
customSubject = "" | |
End If | |
If customSubject <> "" Then | |
subject = customSubject | |
Else | |
subject = olMail.subject | |
End If | |
With objTask | |
If addRecipient Then | |
.subject = olMail.Recipients.Item(1) & ": " & subject | |
Else | |
.subject = subject | |
End If | |
.categories = categories | |
.Body = olMail.Body | |
End With | |
objTask.Save | |
Set objTask = Nothing | |
Set olMail = Nothing | |
Set olNS = Nothing | |
End Sub | |
' Wrapper that gets the current item and calls the previous function, to use as a macro | |
Sub MakeWaitingForTaskWithAttachmentFromCurrentMessageMacro() | |
Dim curMail As Outlook.MailItem | |
Set curMail = GetCurrentItem() | |
Call MakeWaitingForTaskWithAttachmentFromCurrentMessage(curMail) | |
End Sub | |
' From http://www.outlookcode.com/codedetail.aspx?id=50 | |
Function GetCurrentItem() As Object | |
Dim objApp As Outlook.Application | |
Set objApp = CreateObject("Outlook.Application") | |
On Error Resume Next | |
Select Case TypeName(objApp.ActiveWindow) | |
Case "Explorer" | |
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1) | |
Case "Inspector" | |
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem | |
Case Else | |
' anything else will result in an error, which is | |
' why we have the error handler above | |
End Select | |
Set objApp = Nothing | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment