none
VBA Script not working in Rule RRS feed

  • Question

  • I've created a rule that has the RUN A SCRIPT option.

    It ran the first time, I tried it and hasn't run since (several restarts of Office 2010) and Windows 7 reboots.

    I've read the other threads and have done what was suggested, like make sure MACROS are enabled.

    This didn't work either.

    Here is the code I was trying to execute.


    Sub srtnVTEXT(MyMail As MailItem)
    Dim strID As String
    Dim olNS As Outlook.NameSpace
    Dim msg As Outlook.MailItem

    strID = MyMail.EntryID
    Set olNS = Application.Session
    Set msg = olNS.GetItemFromID(strID)
    ' do stuff with incoming msg, e.g.
    strAddress = msg.SenderEmailAddress
    Set Mailobj = Application.CreateItem(olMailItem)
    With Mailobj
    .To = "sampleemailaddr@gmail.com"
    .Body = "You've Got Mail from " & strAddress
    .Send
    End With

    Set Mailobj = Nothing
    Set msg = Nothing
    Set rply = Nothing
    Set olNS = Nothing
    End Sub

    I even tried some more simple to see if it would work. 

    Sub CustomMailMessageRule(Item As Outlook.MailItem)
       MsgBox "Mail message arrived: " & Item.Subject
    End Sub

    Neither are working.

    Does anyone have some other suggestions on why it isn't working?

    Thanks,

    MEG

    Thursday, July 26, 2012 3:45 AM

Answers

  • With macros enabled in an Outlook 2010 instance I added your code to the ThisOutlookSession class:
     

    Sub CustomMailMessageRule(Item As Outlook.MailItem)
       MsgBox "Mail message arrived: " & Item.Subject
    End Sub

    I created a dummy rule to call the script when any email arrived from a person who had sent me some emails that were in my Inbox. I then ran the rule manually and the MsgBox displayed.

    My trust center settings were to have notifications for all macros and not to apply macro settings in the Macro Settings tab.

    Can you run any macros at all in your VBA project? What if you tried on something like this:
     
    Public Sub Test1
        MsgBox "Test1"
    End Sub

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "AGI_MEG" <=?utf-8?B?QUdJX01FRw==?=> wrote in message news:50c9906c-14f4-4eed-ae48-72d260e404bc...

    Ken:

    I really appreciate you hanging with me on this one.

    I checked the APPLY MACRO SECURITY SETTINGS to INSTALLED ADD-INS.  That still didn't make a difference.

    When run manually, it doesn't error out. For example the simple message one, runs, doesn't error out, but yet doesn't "pop up" the message.

    This is beyond my expertise.  I'm about ready to throw in the towel...unless you have another thought or two.

    MEG


    Ken Slovak MVP - Outlook
    • Marked as answer by AGI_MEG Wednesday, August 1, 2012 1:10 PM
    • Unmarked as answer by AGI_MEG Wednesday, August 1, 2012 1:11 PM
    • Marked as answer by AGI_MEG Wednesday, August 1, 2012 1:15 PM
    Monday, July 30, 2012 4:15 PM
    Moderator

All replies

  • where have you declared those routines? in thisoutlooksession module?
    Thursday, July 26, 2012 8:14 AM
  • Correct.

    Sorry, forgot to mention that detail.

    MEG

    Thursday, July 26, 2012 1:07 PM
  • try to change parameter from MailItem to Object and check inside your routine if this is really mailitem.
    Thursday, July 26, 2012 1:26 PM
  • Is the rule running at all? What happens if you select it in the UI and run it manually? Do you hit a breakpoint or MsgBox line in your macro script if you put one there?
     
    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "AGI_MEG" <=?utf-8?B?QUdJX01FRw==?=> wrote in message news:937007a8-2202-4db7-bc1e-a49b96d62379...

    Correct.

    Sorry, forgot to mention that detail.

    MEG


    Ken Slovak MVP - Outlook
    Thursday, July 26, 2012 1:39 PM
    Moderator
  • Ken:

    Thanks for the response.

    This macro is in the ThisOutlookSession.

    When I try to RUN the macro, the dialog box doesn't show any routines.

    The macros in: shows Project1 (VbaProject.OTM).  Even changing this to ALL PROJECTS doesn't make a difference.  Still no macros shown.

    MEG

    Thursday, July 26, 2012 2:00 PM
  • A macro that will show up in the Outlook Macros window has to be a Sub that takes no arguments.
     
        Sub Foobar
     
    would show up,
     
        Sub Folbar(o As MailItem)
     
    would not.
     
    The signature of a sub for a rule script must have the MailItem argument, so it will never show up at all in the Macros dialog.
     
    I asked about running the rule manually from the Rules organizer, not about running any macros.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "AGI_MEG" <=?utf-8?B?QUdJX01FRw==?=> wrote in message news:795dbc76-743d-469d-b331-b60f7f985018...

    Ken:

    Thanks for the response.

    This macro is in the ThisOutlookSession.

    When I try to RUN the macro, the dialog box doesn't show any routines.

    The macros in: shows Project1 (VbaProject.OTM).  Even changing this to ALL PROJECTS doesn't make a difference.  Still no macros shown.

    MEG


    Ken Slovak MVP - Outlook
    Thursday, July 26, 2012 2:53 PM
    Moderator
  • Ken:

    It doesn't run the script from the Rules organizer.

    I'm at a loss.

    I've followed steps on multiple threads on how to do this.  Seems very straightforward.

    MEG

    Thursday, July 26, 2012 5:59 PM
  • If you run the rule manually from the rules organizer does it run? Does it show any errors?
     
    If you change your script (Sub) to show a MsgBox at the start of the code does the MsgBox get shown?

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "AGI_MEG" <=?utf-8?B?QUdJX01FRw==?=> wrote in message news:2e5f562d-c38c-4567-a887-dc7d099d5a5d...

    Ken:

    It doesn't run the script from the Rules organizer.

    I'm at a loss.

    I've followed steps on multiple threads on how to do this.  Seems very straightforward.

    MEG


    Ken Slovak MVP - Outlook
    Thursday, July 26, 2012 6:42 PM
    Moderator
  • Ken:

    Changed it to this simple message Sub:

    Sub CustomMailMessageRule(Item As Outlook.MailItem)
       MsgBox "Mail message arrived: " & Item.Subject
    End Sub

    That didn't change anything.

    So I looked at the TRUST CENTER Settings.  I have enabled all macros checked. but no APPLY MACRO SECURITY SETTINGS to INSTALLED ADD-INS.  Is the VBA classifed as an ADD-IN?

    Thanks,

    MEG

    Thursday, July 26, 2012 7:25 PM
  • Technically, yes VBA for Outlook is implemented as a COM addin. It's installed for all users and registered in the HKLM hive of the registry.
     
    I'm unclear about one thing. Have you actually tried running the rule manually? Did it run without error even though the rule calls for running the script?

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "AGI_MEG" <=?utf-8?B?QUdJX01FRw==?=> wrote in message news:9a0efce5-50c8-4bbf-84f8-4a735536481e...

    Ken:

    Changed it to this simple message Sub:

    Sub CustomMailMessageRule(Item As Outlook.MailItem)
       MsgBox "Mail message arrived: " & Item.Subject
    End Sub

    That didn't change anything.

    So I looked at the TRUST CENTER Settings.  I have enabled all macros checked. but no APPLY MACRO SECURITY SETTINGS to INSTALLED ADD-INS.  Is the VBA classifed as an ADD-IN?

    Thanks,

    MEG


    Ken Slovak MVP - Outlook
    Thursday, July 26, 2012 9:47 PM
    Moderator
  • Ken:

    I really appreciate you hanging with me on this one.

    I checked the APPLY MACRO SECURITY SETTINGS to INSTALLED ADD-INS.  That still didn't make a difference.

    When run manually, it doesn't error out. For example the simple message one, runs, doesn't error out, but yet doesn't "pop up" the message.

    This is beyond my expertise.  I'm about ready to throw in the towel...unless you have another thought or two.

    MEG

    Saturday, July 28, 2012 4:48 AM
  • With macros enabled in an Outlook 2010 instance I added your code to the ThisOutlookSession class:
     

    Sub CustomMailMessageRule(Item As Outlook.MailItem)
       MsgBox "Mail message arrived: " & Item.Subject
    End Sub

    I created a dummy rule to call the script when any email arrived from a person who had sent me some emails that were in my Inbox. I then ran the rule manually and the MsgBox displayed.

    My trust center settings were to have notifications for all macros and not to apply macro settings in the Macro Settings tab.

    Can you run any macros at all in your VBA project? What if you tried on something like this:
     
    Public Sub Test1
        MsgBox "Test1"
    End Sub

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "AGI_MEG" <=?utf-8?B?QUdJX01FRw==?=> wrote in message news:50c9906c-14f4-4eed-ae48-72d260e404bc...

    Ken:

    I really appreciate you hanging with me on this one.

    I checked the APPLY MACRO SECURITY SETTINGS to INSTALLED ADD-INS.  That still didn't make a difference.

    When run manually, it doesn't error out. For example the simple message one, runs, doesn't error out, but yet doesn't "pop up" the message.

    This is beyond my expertise.  I'm about ready to throw in the towel...unless you have another thought or two.

    MEG


    Ken Slovak MVP - Outlook
    • Marked as answer by AGI_MEG Wednesday, August 1, 2012 1:10 PM
    • Unmarked as answer by AGI_MEG Wednesday, August 1, 2012 1:11 PM
    • Marked as answer by AGI_MEG Wednesday, August 1, 2012 1:15 PM
    Monday, July 30, 2012 4:15 PM
    Moderator
  • Ken:

    I set my settings as you mention above (they were like that except I had the checkbox for the INSTALLED ADD-INS and then exited.

    When I restarted OUTLOOK, the rule started working.  It is a mystery to me.

    I appreciate your staying with me on this one.  You went way beyond the call of duty!!!

    Thanks again,

    MEG

    Wednesday, August 1, 2012 1:15 PM
  • I have a similar problem. I created a script that when I receive an email, the attachment, if of a certain file type, will be saved to a folder on my work server. It worked very well the first day. When I tried it the next day, it would not work at all. I found a tip about deleting VbaProject.OTM and starting over. This worked, and again after closing Outlook for the day and trying again the next day, it failed.

    The purpose of the script is to help us with our work flow, deleting the rule and script then re-writing both every morning cannot be the best solution.

    Here is the script:

    Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    saveFolder = "B:\ColorBC\mxml\"
         For Each objAtt In itm.Attachments
        If InStr(objAtt.DisplayName, ".mxml") Then
                  objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
              End If
              Set objAtt = Nothing
         Next
    End Sub

    Thursday, June 16, 2016 11:35 PM
  • Have you looked through the thread to make sure your macro and trust settings are the same as those that worked for the original poster in this thread?

    Ken Slovak MVP - Outlook

    Friday, June 17, 2016 1:51 PM
    Moderator
  • Same issue. Very frustrating. All trust settings are correct. There is an outlook but it seems.

    Benny

    • Proposed as answer by said2be Tuesday, February 21, 2017 9:26 AM
    • Unproposed as answer by said2be Tuesday, February 21, 2017 9:27 AM
    Wednesday, August 24, 2016 1:30 AM
  • here is the catch:

    Do Not put any code outside of your Sub. no D

    Tuesday, February 21, 2017 9:28 AM