Setting importance via VBA using rules wizard

B

Bongo

My incoming messages come with the Importance flag both set and unset. Since
I sort by importance, having messages with unset importance float to the
bottom of my list (after hundreds with Normal importance).

I have created the following VBA program and have it invoked from rule that
applies to all incoming messages:

Sub SetImportanceRule(Item As Outlook.MailItem)
If Item.Importance <> olImportanceHigh Then
Item.Importance = olImportanceNormal
Item.Save
End If
End Sub

I signed the VBA module so that there would be no macro warning message on
first invocation.

Nonetheless, occasionally the script fails, and (a) the message importance
isn't set correctly; and (b) the rule turns itself off. Noting that this
occurred with return receipts often, I changed the type of the argument to to
Object instead of Outlook.MailItem, but the error still occurs. I can't
determine what characteristics of a particular message give rise the error
occuring.

Any thoughts on (1) how to determine what error is occuring (the diagnostics
are nonexistant); (2) what might be wrong; (3) how to define an error handler
so that it fails gracefully instead of causing the rule to turn off?

Tx & rgds
 
E

Eric Legault [MVP - Outlook]

You cannot use Object as an argument; it must be MailItem or AppointmentItem:

How to create a script for the Rules Wizard in Outlook:
http://support.microsoft.com/default.aspx?scid=KB;en-us;q306108

What is the error message that you are receiving? I don't see anything that
is obviously wrong with your code; all you want it to do is change the
importance of messages from low to normal, correct?

Also, if you want to trap for return receipts, try looking at the
MessageClass property ("IPM.Note.NDR").
 
B

Bongo

What is the error message that you are receiving?

When it's run from the Rules Wizard, no error is shown other than a generic
script error from the rules wizard. That's why I was hoping to put in an
error handler (Eric, is this possible?) to both find out what the error is
and to prevent the rule from being deactivated.

When I run the code from a sub on a bunch of messages direclty (so the
messages aren't arguments, but local variables) I sometimes get a type
mismatch error (type 13). This seems to happen with receipts, but can be
avoided by using Object instead of the Outlook.MailItem.

I am now testing a solution where I screen the applicaiton of the rule to
Messages and Encrypted Messages, which should prevent it from being invoked.
Is there any source for the classes assigned to each type of item that can
show up in an inbox?

Thanks for your response.
 
E

Eric Legault [MVP - Outlook]

If you want to put in an error handler, do as you normally would for any
regular procedure (On Error Goto MyErrorHandler, then evaluate Err.Number).
You can't disable or enable the rule with code though.

Do not use a generic Object variable, as attempting to cast it to a variable
declared against a defined Outlook Item object can be problematic. Check the
MessageClass property against these expected values and cast to the
appropriate item:

Item types and message classes
http://msdn.microsoft.com/library/e...typesmessageclasses_HV01044391.asp?frame=true
 
B

Bongo

Thanks for the tip, Eric. Based on that, I've been using the following
procedure (and this is restricted to handling items of Message and Encrypted
Message by the rule itself):

Sub SetImportanceRuleMessage(Item As Outlook.MailItem)
On Error GoTo ErrorHandler
If Item.Importance <> olImportanceHigh Then
Item.Importance = olImportanceNormal
Item.Save
End If
Exit Sub
ErrorHandler:
MsgBox (Err.Number & " " & Err.Description & " " & Err.Source)
End Sub

It still fails, and in the same way as before: I get the script failure
window from the rules wizard, with a generic unhelpful message (I forget what
it says, something like "Could not execute"), a standard Outlook-generated
message box. My error handler seems not to execute (it certainly doesn't
produce a message box).

That makes me believe that my routine never is getting invoked when the
error occurs . . . any thoughts?

Tx & rgds
 
B

Bongo

No matter what I did (error handlers, specifying separate rules for different
message types), I couldn't get it to stop crashing out on the scripts and
disabling the rules.

Starting with Sue Mosher's code at
http://www.outlookcode.com/d/code/quarexe.htm, I added the follwing event
handler to ThisOutlookSession (note: this intentionally sets Importance to
Normal for messages with no Importance as well as Low Importance):

Private WithEvents olInboxItems As Items

Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.GetNamespace("MAPI")
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
Set objNS = Nothing
End Sub

Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
On Error Resume Next
Debug.Print "In"
If Item.Class = olMail Or Item.Class = olReport Then
If Item.Importance <> olImportanceHigh Then
Item.Importance = olImportanceNormal
Item.Save
End If
End If
On Error GoTo 0
End Sub

Thanks Sue!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top