Problem Getting Outlook 2007 Running VBA Script

I am trying to get Outlook to save an attachment in a daily email to a folder where I can have a file system watcher ready to parse and analyze the attachment (this is a data integrity check report). I created a rule that should run a VBA script, but it just doesn't work as far as I can tell. I have verified in VB6 that the code will actually save some text in the file, so if Outlook does indeed run the VBA script, it should be able to do the same. But this is not the case! Anyone can figure out what I am doing wrong?

Dim WithEvents objInbox As Outlook.Items

Private Sub Application_Startup()
   Set objInbox = Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Sub SnagAttachment(theItem As MailItem)
    On Error Resume Next
    Dim fnum As Integer
    fnum = FreeFile()
    Open "c:\temp\success.txt" For Output As #fnum
    Print #fnum, "Ran SnagAttachment Successfully"
    Close #fnum
End Sub

      

Note that when I use the rule wizard and select "run a script", Sub SnagAttachment is displayed as a selectable script.

+1


a source to share


4 answers


How do you know if it works or not when you On Error Resume Next

start the procedure? You will never know.

Here are the rules for creating a script to be executed as part of the rule:

How to create script for rule wizard in Outlook



Also note the disclaimer found in How to handle incoming messages in Microsoft Outlook :

A "run script rule" is not a good choice for heavy application traffic, as Outlook is likely to skip applying the rule if there are many items that match the rule's conditions.

+5


a source


To get the script to work, you need to change the security settings in Outlook. Go to Tools> Macro> Security and change it to Alerts for all macros. Then restart Outlook.



Hope it helps

+2


a source


Try to isolate the exact problem:

  • Check your macro security settings. The maximum should be set no higher than the "Warnings for all macros".
  • Try to create a new module with one test sub:
    Sub Test (Item as Outlook.MailItem)
    MsgBox "test"
    End Sub
    
    Then set up a new rule to handle _all_ incoming messages that trigger this sub as the only action, and temporarily disable all other rules. Then send a message to yourself. If you don't get a pop-up as a result, it may indicate a poor Outlook installation. Try reinstalling it or call MS directly for support.
  • If the previous test was successful, try using the `Scripting.FileSystemObject` object instead of` Freefile () `to create and populate files. It's just to check if there is some strange error you are encountering here. Should I shoot, right?
  • Make sure the terms of your rule are set correctly. There might be a glitch or a typo in the state that just throws away any messages you want the script to run.
0


a source


I got the same problem and it seems to me that if there is an error in your code the script won't even run. This is in contrast to standard VBA where the debugger pops up for runtime errors. For example, I had a label at the end of my function, after which the colon was missing. This caused the script to not run at all (as opposed to going to that line and then crashing). I would suggest commenting out all of your code and starting with msgbox "hello world". I would leave this in your code when you debug it so that I know the code is running, but you probably have to dismiss the message box many times. Iterately add back lines of code until you find where the problem is.

0


a source







All Articles