jdabramson
04-21-2016, 01:56 PM
I would like to alter the script below to allow a outbound message trigger rule to create a task based text in a outbound email. Here's the proposed rule
Apply this rule after I send the message with "specific words" in the subject or body and on this computer only run "a script"
My goal is to have a the script,below, triggered via rule or VBA code when I type zzwo in the message. This will create a task from the email message in a "waiting on reply" category. The script, below, will allow me to create an inbound rule to trigger it; however, inbound rules do not show this script as an option to run.
' 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
My thanks for your replies and Diego Zamboni for the original code.
Apply this rule after I send the message with "specific words" in the subject or body and on this computer only run "a script"
My goal is to have a the script,below, triggered via rule or VBA code when I type zzwo in the message. This will create a task from the email message in a "waiting on reply" category. The script, below, will allow me to create an inbound rule to trigger it; however, inbound rules do not show this script as an option to run.
' 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
My thanks for your replies and Diego Zamboni for the original code.