Reputation: 629
I am trying to get the text value(s) of the conditions for a rule that I have set up in MS Outlook.
I have created a rule named "TestRule" with a condition based on text in the message Body (it must have the text "zzz" in it) and also a condition based on text in the Message Header (it must have the text "aaa" in it). (Of course, this rule will probably never run, this is just to test for whether I can read the conditions in the rule.) The rule and the conditions are enabled.
Here is the code that I am using
Dim olRules As Outlook.Rules
Dim olRule As Outlook.Rule
Set olRules = Application.Session.DefaultStore.GetRules
Set olRule = olRules.Item("TestRule")
Debug.Print olRule.Conditions.Body.Text
Debug.Print olRule.Conditions.MessageHeader.Text
However, both debug.print lines give an error of "Type mismatch".
How can I read the current value of the conditions?
(I have checked and double-checked: there is a rule named "TestRule".)
Upvotes: 2
Views: 1757
Reputation: 1661
GD DRC,
I've putten some more effort into your problem and I think I've figured out why you get the type mismatch.
You have likely added some keywords to the "body" condition as well as to the "messageheader" condition ? These individual entries are added to an array, the "olRule.Conditions.Body.Text" array.
Your below lines of code;
olRule.Conditions.Body.Text
Debug.Print olRule.Conditions.MessageHeader.Text
are essentially asking the 'debug.print' mehtod to print an array, yet it does not know how to do that. As it does not understand which part of the array to print. Depending on the amount of entries you have made the identifier of each entry will necessarily require an identifier to point to the unique array entry.
So:
There are a couple of ways to solve your coding issue:
Set a breakpoint on the line:
olRule.Conditions.Body.Text
So that execution stops just prior to executing the line of code. Open your 'Locals Window' (through View|Locals Window) and open olRule.Text and you will see as many array entries as you have added.
Main subroutine
Sub TestRule()
Dim olRules As Outlook.Rules
Dim olRule As Outlook.Rule
Set olRules = Application.Session.DefaultStore.GetRules
Set olRule = olRules.Item("TestRule")
Debug.Print TypeName(olRule)
printArray olRule.Conditions.Body.Text
printArray olRule.Conditions.MessageHeader.Text
End Sub
printArray subroutine
Private Sub printArray(ByRef pArr As Variant)
Dim readString As Variant
If (IsArray(pArr)) Then 'check if the passed variable is an array
For Each readString In pArr
If TypeName(readString) = "String" Then 'check if the readString is a String variable
Debug.Print readString
End If
Next
End If
End Sub
You could also integrate the second subroutine in the first but it is better coding practice do perform repetative tasks either in a Function or in a separate subroutine (you could then use it for other methods in same module as well :-))
Trust this will provide you with the solution !
Goodluck !
Upvotes: 1
Reputation: 1661
GD DRC,
Are you sure there is a rule by the name "TestRule" existent in your default store.
If I execute your above code I get perfect result (though nothing gets printed as the values are not set. So the syntax in your above code is fine.
Try adding:
Debug.Print TypeName(olRule)
The result in your Immediate Window should be "Rule" if your olRule is actually set.
So overall code should be:
Dim olRules As Outlook.Rules
Dim olRule As Outlook.Rule
Set olRules = Application.Session.DefaultStore.GetRules
Set olRule = olRules.Item("TestRule")
Debug.Print TypeName(olRule)
Debug.Print olRule.Conditions.Body.Text
Debug.Print olRule.Conditions.MessageHeader.Text
Hope that will guide you in the right direction !
Cheers !
Upvotes: 0