Consulting

Results 1 to 4 of 4

Thread: Want to forward a template email with an email address contained in the trigger email

  1. #1
    VBAX Newbie
    Joined
    Oct 2010
    Posts
    2
    Location

    Want to forward a template email with an email address contained in the trigger email

    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?


    [VBA]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
    [/VBA]


    Thank you!

  2. #2
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    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:

    [VBA]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[/VBA]
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  3. #3
    VBAX Newbie
    Joined
    Oct 2010
    Posts
    2
    Location
    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?

    [VBA]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[/VBA]

  4. #4
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    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?
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •