PDA

View Full Version : Want to forward a template email with an email address contained in the trigger email



ayrkain
10-29-2010, 09:18 AM
I'm trying to automate a process wherein an email comes in with a certain Subject, then a regex pulls an email address out of that trigger email, and sends a template to that email address. What I have so far is somewhat mangled. Please help?


Sub CustomMailMessageRule(Item As Outlook.MailItem)
Dim myolApp As Outlook.Application
Dim myItem As Outlook.MailItem
Dim mySend As Outlook.MailItem
Set myNamespace = myolApp.GetNamespace("MAPI")
Set myFolder = myNamespace.GetDefaultFolder(olFolderInbox)
Set myolApp = CreateObject("Outlook.Application")
If Not myFolder.Items = 0 Then
Set myItem = myFolder.Items(1)
myItem.BodyFormat = olFormatPlain
Set mySend = myolApp.CreateItemFromTemplate("C:\template.oft")
With mySend
.Recipients = RE6(myItem.Body)
.Subject = "Blahdy blah"
End With
mySend.Send
End If

End Sub

Function RE6(strData As String) As String
Dim RE As Object, REMatches As Object
Set RE = CreateObject("vbscript.regexp")
With RE
.MultiLine = True
.Global = False
.IgnoreCase = True
.Pattern = "(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)" & _
"|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}"
End With

Set REMatches = RE.Execute(strData)
RE6 = REMatches(0)

End Function



Thank you!

JP2112
11-01-2010, 07:45 AM
When your rule is triggered, it runs the VBA code?

If so, the code is passed a copy of the mail item that triggered the rule. So you don't need to grab the first item in the Inbox.

Also, it's not clear why you need to change the body format for that email.

Here's what I came up with:

Sub CustomMailMessageRule(Item As Outlook.MailItem)
Dim myolApp As Outlook.Application
Dim mySend As Outlook.MailItem
Set myolApp = Outlook.Application
myItem.BodyFormat = olFormatPlain
Set mySend = myolApp.CreateItemFromTemplate("C:\template.oft")
With mySend
.Recipients = RE6(Item.Body)
.Subject = "Blahdy blah"
End With
mySend.Send
End If

End Sub

ayrkain
11-03-2010, 03:15 PM
Now it compiles perfectly, but doesn't do anything. I've pared it down a bit based on suggestions here and elsewhere. Anything obvious I'm missing here?

Sub CustomMailMessageRule(Item As Outlook.MailItem)
Dim myolApp As Outlook.Application
Set myolApp = Application
Dim mySend As Outlook.MailItem
' myItem.BodyFormat = olFormatPlain
Set mySend = myolApp.CreateItemFromTemplate("C:\template.oft")
With mySend
.Recipients = RE6(Item.Body)
.Subject = "Template"
End With
mySend.Send
End Sub

Function RE6(strData As String) As String
Dim RE As Object, REMatches As Object
Set RE = CreateObject("vbscript.regexp")
With RE
.MultiLine = True
.Global = False
.IgnoreCase = True
.Pattern = "(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)" & _
"|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}"
End With

Set REMatches = RE.Execute(strData)
RE6 = REMatches(0)

End Function

JP2112
11-03-2010, 06:30 PM
Hard to say. I assume you changed "C:\template.oft" to point to the actual location of the template?

Have you tried setting a breakpoint in the code to step through it?

Have you tested your regex function to make sure it works?