Outlook 2010 - Compile\Run script works in editor, doesnt work when applied to rule RRS feed

  • Question

  • Hi Everyone! I'm turning here for help as I don't think I have anywhere else to go...

    Ok...long story short - I have no experience in programming at all, but managed to piece together 2 existing scripts to get the following :

    I work as an Tier 2 analyst and would like to take the Service Desk's emails (with Summary: and End User: fields), and send a blank email with just the parsing from the orig. email in the subject line.

    Example -

    This message is to notify you that Incident 2536677 has been assigned to the XXXXXX IS Tech Support.

    Affected Service:

    Open Date:
    Incident Area:
    Response Target:
    Resolution Target:
    Summary: Security Alert Message on Startup

    End User: Jane Smith

    So my end result would be an email with "Ticket - Security Alert Message on Startup - Jane Smith" as the subject line. This would be ran in a rule that would take the incoming email, send it to a Ticket Alerts folder and then run the script.

    It works like a charm when I compile and run on a selected email with the above criteria from within the editor. What I am currently getting when sending test emails to it is an email with "Ticket - " and that's it. It appears it is now not appending the "testSubject" results. I've done a lot of looking around but at this point I am just wasting hours on hours as I don't have the programming knowledge required to determine what exactly the issue is. So I know for a fact the script is running as it created the new email and adds the "Ticket - " string I put in myself.

    This is in Module1, also there were a few items commented out, as they were not needed, or so my semi-smart programmer coworker had said. I also edited out my current email address with a fake one.

    Any help would be appreciated, Or even pointing me in the correct direction.

    Sub Test(item As Outlook.MailItem)
    Call GetValueUsingRegEx
    End Sub

    Sub GetValueUsingRegEx()
        Dim olMail As Outlook.MailItem
        Dim Reg1 As RegExp
        Dim M1 As MatchCollection
        Dim M As Match
        Dim strSubject As String
        Dim testSubject As String
        Set olMail = Application.ActiveExplorer().Selection(1)
        Set Reg1 = New RegExp
    For i = 1 To 2
    With Reg1
        Select Case i
        Case 1
            .Pattern = "(Summary[:]([\w-\s]*)\s*)\n"
            .Global = False
        Case 2
           .Pattern = "(End User[:]([\w-\s]*)\s*)\n"
           .Global = False
           End Select
    End With
        If Reg1.Test(olMail.Body) Then
            Set M1 = Reg1.Execute(olMail.Body)
            For Each M In M1
                Debug.Print M.SubMatches(1)
                strSubject = M.SubMatches(1)
             strSubject = Replace(strSubject, Chr(13), "")
             testSubject = testSubject & "- " & Trim(strSubject)
             Debug.Print i & testSubject
        End If
    Next i
    'Debug.Print olMail.Subject & testSubject
    'Create a new message
        Set objOutlk = CreateObject("Outlook.Application")
        Set objMail = objOutlk.CreateItem(olMailItem)
        objMail.To = MYSELF @ MYSELF. com
    'Set up Subject Line
        objMail.Subject = "Ticket - " & testSubject
    'strMsg = " " & vbCrLf
        'objMail.Body = strMsg
        objMail.Send 'Use this to display before sending, otherwise call objMail.Send to send without reviewing
    'olMail.Subject = olMail.Subject & testSubject
    Set Reg1 = Nothing
    End Sub



    Wednesday, April 23, 2014 7:12 PM

All replies

  • Hello Andrew,

    There is no need to create a new APplication instance if you run the VBA macro in Outlook:

     Set objOutlk = CreateObject("Outlook.Application")

    Instead, you need to use the global Application property. 

    I'd suggest trying to call the Save method before sending a message. Does it help?

    Wednesday, April 23, 2014 7:29 PM
  • Again, I have little to no experience, and have no idea what you are meaning.

    I commented out the line you mentioned above and now get a run-time error 424, object required.

    Also, I don't understand why I have to create a 2nd sub\end section calling the first, otherwise it doesn't populate in Outlooks list of Macros. I can't seem to get the "Sub GetValueUsingRegEx ()" working with the outlook.mailitem but that is not my biggest concern. The fact that it runs and works properly in the vb editor but does not when I use it as a rule makes me scratch my head. If it compiles and runs why wouldn't it act the exact same way when the script is called from Outlook?

    See if you can try and recreate the issue on your machine - there is likely something laughably obvious I am missing. But it DOES run, just the portion of the script that takes the text strings above and adds them the the subject of the new email isn't working. "objMail.Subject = "Ticket - " & testSubject"

    Thanks in advance!

    Wednesday, April 23, 2014 7:45 PM
  • Andrew,

    Your code doesn't work with the rule because you didn't use the item passed to the Test sub. Instead, you get a selected item in the Explorer window.

     Set olMail = Application.ActiveExplorer().Selection(1)

    That's why you can't get it working. Use the item passed as a parameter.

    'Create a new message
    Set objMail = Application.CreateItem(olMailItem)
    objMail.To = MYSELF @ MYSELF. com
    'Set up Subject Line
    objMail.Subject = "Ticket - " & testSubject
    'strMsg = " " & vbCrLf
    'objMail.Body = strMsg

    Wednesday, April 23, 2014 7:52 PM
  • I should mention - When I manually compile\run the rule, any selected email in outlook will run and send a new email with the proper subject, but when I setup the rule to call the script (instead of running it from within the Outlook vb editor), it seems to be missing the testSubject appending to the subject.

    Wednesday, April 23, 2014 7:52 PM
  • So I commented the "Set olMail = Application.ActiveExplorer().Selection(1)" out and made the changes indicated above and still have the same problem. The script still runs and appears to operate the same way with your changes.

    Running the script in the vb editor works fine on a selected email, and I receive a new email with what I want, but when ran thru the rules (which I completely removed and just have the script running now on emails meeting the criteria) it does not add the subject strings taken.

    Wednesday, April 23, 2014 8:30 PM
  • Most probably the testSubject string is empty when you rule is run. Is this the case?
    Wednesday, April 23, 2014 8:41 PM
  • Would it not have the same issues manually running the script on an email selected? The testSubject string is not empty when the script is run manually on an email.

    Again, I am new to this so I would not know how to check if the string was empty.

    Wednesday, April 23, 2014 8:51 PM
  • Please take a look at the Getting Started with VBA in Office 2010 guide in MSDN. It describes the basics of VBA programming to get started quickly.

    You can use the Debug.Print method for checking the value at runtime.

    Thursday, April 24, 2014 11:54 AM