' Set the string comparison method to Text ("AAA" = "aaa").
Option Compare Text
Sub CreateWOTaskFromEmail(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 on Reply"
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 = "zzwo (.*)"
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 CreateWOTaskFromEmailMacro()
Dim curMail As Outlook.MailItem
Set curMail = GetCurrentItem()
Call CreateWOTaskFromEmail(curMail)
End Sub
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