1
votes

I'm trying to get Outlook to save the attachment in a daily email to a folder where I can have a file system watcher ready to parse and analyze the attachment (it's the report of a data integrity checker). I've set up a Rule that is supposed to run a VBA script, but it just doesn't run as far as I can tell. I've verified in VB6 that the code will in fact save some text to a file, so if Outlook actually runs the VBA script it should be able to do the same. But it doesn't! Can anyone see what the heck I'm 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 Rules wizard, and choose "run a script" the Sub SnagAttachment is listed as a script that can be selected.

4
wow, can't belive no one has helped you with this yet. if it is still an issue for you, have you gotten any further than the code above? i'm not exactly why you're using the Application_Startup event if you're running a script (assuming) when a new mail arrives that has an attachment.Todd Main
Thanks for chiming in! No help for it, apparently, and the above code was pulled from MSDN I believe, so it has the stamp of officialdom ... sort of. But the issue I needed the information for has made this question obsolete for my purposes, because I don't need to know it any longer (we replaced the system I would have used the answer for). Nevertheless, it is still of interest to me, since I would really like to be able to do custom things for Outlook messages arriving.Cyberherbalist
Do you still need a solution? Have you tried any of the answers?JimmyPena

4 Answers

6
votes

How would you know if it's working or not when you put On Error Resume Next at the top of the procedure? You would never find out.

Here are the rules for creating a script that should be run as part of a Rule:

How to create a script for the Rules Wizard in Outlook

Also note the caveat found at How to process incoming messages in Microsoft Outlook:

A "run a script" rule is not a good choice for heavy traffic applications, as Outlook is likely to skip applying the rule if too many items arrive that meet the rule's conditions.

2
votes

In order to get the script to work you need to change the security settings in Outlook. Go to Tools > Macro > Security and change it to "Warnings for all macros". Then restart Outlook.

Hope this helps

0
votes

Try isolating the exact problem:

  1. Check macro security settings. At maximum, it must be set no higher than "Warnings for all macros".
  2. Try creating a new module with a single test sub:
    Sub Test(Item as Outlook.MailItem)
    MsgBox "test"
    End Sub
    
    Then set up a new rule to handle _all_ incoming messages running this sub as the only action, and temporarily disable all other rules. Then send a message to yourself. If you get no popup box as a result, this may be an indication of a bad Outlook install. Try reinstalling it, or calling MS up directly for support.
  3. If the previous test was successful, try working with the `Scripting.FileSystemObject` object instead of `Freefile()` to create and populate files. This is just to test if there's some odd bug you're encountering here. Worth a shot, right?
  4. Make sure your rule conditions are set correctly. There could be a glitch or misspelling in a condition which just drops all messages you want this script to run on.
0
votes

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