15

I'm trying to debug an Outlook 2007 VBA script that's fired by a rule. I've set a breakpoint in the script but it doesn't get hit.

The script is actually a Sub in the ThisOutlookSession object.

When I run the rule on a specified folder nothing seems to happen.

What am I doing wrong?

Update:

I've added a MsgBox "Processing: " & mailItem.Subject to the script and that pops up just fine when I run the rule. However I can't seem to get the script to stop on breakpoints.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Kev
  • 118,037
  • 53
  • 300
  • 385
  • 2
    The MsgBox is a good idea. I too would like to know how to line-debug scripts run inside a rule. – Ryan Lynch May 15 '11 at 21:30
  • I know this question is old, but it's still unanswered. Can you post your code and what conditions would cause it to execute? – JimmyPena Oct 25 '11 at 20:24

2 Answers2

12

I think you may not be doing anything wrong, because I have experienced exactly the same behaviour.

However, in order to debug your VBA, I suggest that you create a macro (via the Tools|Macro|Macros menu) that calls your script function with a test e-mail item that you create in the macro.

Maybe something like this:

Sub TestScript()
    Dim testMail As MailItem
    Set testMail = Application.CreateItem(olMailItem)
    testMail.Subject = "Test subject"
    testMail.Body = "Test body"
    Project1.ThisOutlookSession.YourScriptForDebugging testMail
End Sub

This way you can "Step Into" the macro via that Macro dialog again, and do all the debugging you need. It solved my problem, anyway.

Nick
  • 1,490
  • 1
  • 14
  • 21
  • Thanks for the answer Nick. I've long since upgraded to Outlook 2010 and the problem seen in 2007 doesn't exist in 2010. I can hit breakpoints as happy as Larry now. Your suggest does work on 2010 as well and I would guess would on 2007 as well if it was still around. – Kev Feb 06 '12 at 17:22
  • @Nick I can put breakpoints in the script. But I can't see the values stored in variables and stuff. How do I see that? – Prateek Narendra Feb 14 '17 at 05:59
  • @PrateekNarendra once you have the execution paused at a breakpoint, you can use the "Immediate" window to print the values of any variable or expression. – Nick Feb 14 '17 at 13:37
  • @Nick - the best way is to put the variables in the watch window.. I figured it out but thanks for your help – Prateek Narendra Feb 14 '17 at 17:46
  • @PrateekNarendra - You're so right - it's a long time since I was poking around in macro-land and I'd forgotten about that! – Nick Feb 15 '17 at 09:29
  • @Nick - I did all this only to realize at the end that macros arent supported in Outlook Mac :P Looks like itll only work in a few laptops in our team :P – Prateek Narendra Feb 15 '17 at 09:38
0

Any existing item can be used to test code that requires one.

Sub passOpenItem()
    'first open an item
    codeRequiringItemParameter ActiveInspector.CurrentItem
End Sub

Sub passSeletion()
    'first select an item
    codeRequiringItemParameter ActiveExplorer.Selection(1)
End Sub

Sub codeRequiringItemParameter(itm As Object)
    Debug.Print "TypeName: " & TypeName(itm)
    Debug.Print "Class...: " & itm.Class
End Sub
niton
  • 8,771
  • 21
  • 32
  • 52