Hi Folks,
Looking for AHK V1 code that uses the Outlook Component Object Model (COM) to export Outlook 365 Rules to any readable format, such as CSV, HTML, TXT, XLS, XML, whatever. If this is not possible, does anyone know of a third-party tool that does it? As far as I know, Outlook 365 itself cannot do it, but happy to be proven wrong on that. Thanks much, Joe
How to use Outlook Component Object Model (COM) to export Outlook 365 Rules to readable format
- JoeWinograd
- Posts: 2200
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
-
- Posts: 77
- Joined: 06 Oct 2016, 15:57
Re: How to use Outlook Component Object Model (COM) to export Outlook 365 Rules to readable format
Getting rules from outlook is indeed a royal pain. I can give you a short example and point you in the right direction, but you will need to do a fair amount of work to get it all worked out.
If you want to work on this project and flesh it out you will want to use Outlooks built in object browser. To reach it you need to have access to the developer tab, or you can press alt + F11 while in outlook. Then Navigate to View --> Object Browser. From the dropdown at the top change <All Libraries> to Outlook then scroll down the Classes window and find the class you are interested in. I would start with Rule, and RuleCondition to get an idea of what you are looking at. For instance the Enumeration map in my example above came from going to the RuleCondition Class and clicking on the ConditionType Member, and then down at the bottom where it gives information it says "Property ConditonType as OlRuleConditonType" by clicking on the hyperlink "OlRuleConditonType" it will take you to that enumeration . Then you have to click each member to get it's constant value.
You can probably begin to see the level of work needed for this. There will be serveral more enumeration tables to be translated into maps and finding all the different parts of the rule may also be tricky.
Code: Select all
^!c::
ol := ComObjActive("Outlook.Application")
;Enumeration Map for OlRuleConditionType
RuleConditionType := {3:"Account",29:"AnyCategory",13:"Body",14:"Body Or Subject",18:"Category",9:"CC",22:"Date Range",8:"Flagged For Action",23:"Form Name",1:"From",31:"From Any RSS Feed",30:"From RSS Feed",20:"Has Attachment",6:"Importance",27:"Local Machine Only",26:"Meeting Invite Or Update",15:"Message Header",11:"Not To",4:"Only To Me",19:"OOF",28:"Other Machine",24:"Property",16:"Recipient Address",17:"Sender Address",25:"Sender In Address Book",7:"Sensitivity",12:"Sent To",21:"Size Range",2:"Subject",5:"To",10:"To Or CC",0:"Unknown"}
msg := ""
;Get the Rules for the Active outlook session
Rules := ol.ActiveExplorer.CurrentFolder.Store.GetRules()
;Begin looping through them
Loop, % Rules.Count
{
msg .= Rules.Item(OuterDex := A_index).Name "`n"
;Note Each rule has multiple parts like: Conditions, Exceptions, ExecutionOrder etc.
;The following loop is just for ilustration so you know what you are getting into. In practice I would only add items to the msg that are true
Loop, % Rules.Item(A_index).Conditions.Count
{
Condition := Rules.Item(OuterDex).Conditions.Item(A_index)
msg .= " - " RuleConditionType[Condition.ConditionType] " - " (Condition.Enabled = 0 ? "False" : "True" ) "`n"
}
}
MsgBox, % msg
return
You can probably begin to see the level of work needed for this. There will be serveral more enumeration tables to be translated into maps and finding all the different parts of the rule may also be tricky.
- JoeWinograd
- Posts: 2200
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: How to use Outlook Component Object Model (COM) to export Outlook 365 Rules to readable format
Hi @ShatterCoder,
That's a great first step...works perfectly! I care only about the From and Subject fields, so I added a check in your code for RuleConditionType[Condition.ConditionType] being From or Subject, which gives results like this:
Rule 1 Name
- Subject - True
- From - True
Rule 2 Name
- Subject - True
- From - True
But I'm unclear on how to get the value of the Subject and From fields. I'm sure your comment under the code is what I need to understand better, but any additional thoughts on getting just those two fields would be much appreciated! Thanks again, Joe
That's a great first step...works perfectly! I care only about the From and Subject fields, so I added a check in your code for RuleConditionType[Condition.ConditionType] being From or Subject, which gives results like this:
Rule 1 Name
- Subject - True
- From - True
Rule 2 Name
- Subject - True
- From - True
But I'm unclear on how to get the value of the Subject and From fields. I'm sure your comment under the code is what I need to understand better, but any additional thoughts on getting just those two fields would be much appreciated! Thanks again, Joe
-
- Posts: 77
- Joined: 06 Oct 2016, 15:57
Re: How to use Outlook Component Object Model (COM) to export Outlook 365 Rules to readable format
I can give you an example of a Sent To rule (sorry I dont have any rules for a from address on my account and I dont want to mess with my rules on my work account). The From condition should be pretty similar.
Hope this helps. If you get stuck post what you have and I'll see if I can help out
Code: Select all
ol := ComObjActive("Outlook.Application")
;~ if (ol.Rules.Item(1))
;Enumeration Map for OlRuleConditionType
RuleConditionType := {3:"Account",29:"AnyCategory",13:"Body",14:"Body Or Subject",18:"Category",9:"CC",22:"Date Range",8:"Flagged For Action",23:"Form Name",1:"From",31:"From Any RSS Feed",30:"From RSS Feed",20:"Has Attachment",6:"Importance",27:"Local Machine Only",26:"Meeting Invite Or Update",15:"Message Header",11:"Not To",4:"Only To Me",19:"OOF",28:"Other Machine",24:"Property",16:"Recipient Address",17:"Sender Address",25:"Sender In Address Book",7:"Sensitivity",12:"Sent To",21:"Size Range",2:"Subject",5:"To",10:"To Or CC",0:"Unknown"}
msg := ""
;Get the Rules for the Active outlook session
Rules := ol.ActiveExplorer.CurrentFolder.Store.GetRules()
;Begin looping through them
Loop, % Rules.Count
{
msg .= Rules.Item(OuterDex := A_index).Name "`n"
;Note Each rule has multiple parts like: Conditions, Exceptions, ExecutionOrder etc.
;The following loop is just for ilustration so you know what you are getting into. In practice I would only add items to the msg that are true
Loop, % Rules.Item(A_index).Conditions.Count
{
Condition := Rules.Item(OuterDex).Conditions.Item(A_index)
;an example of a SentTo rule
if (Condition.ConditionType = 12)
{
recips := ""
c := Rules.Item(OuterDex).Conditions
loop, % c.SentTo.Recipients.Count
{
item := c.SentTo.Recipients.Item(A_index).AddressEntry
;this next bit is something you will want to pay attention to. MS likes to make life difficult so the address field is sometimes in their wonky exchange format which looks like a bunch of garbage not an actual email address.
if (item.Type = "EX")
address := item.GetExchangeUser.PrimarySmtpAddress
else
address := item.Address
recips .= (A_index = 1 ? "" : "`n" ) item.Name " <" address ">"
}
MsgBox, % recips
}
;~ msg .= " - " RuleConditionType[Condition.ConditionType] " - " (Condition.Enabled = 0 ? "False" : "True" ) "`n"
}
}
;~ MsgBox, % msg
return
- JoeWinograd
- Posts: 2200
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: How to use Outlook Component Object Model (COM) to export Outlook 365 Rules to readable format
Yes, very helpful...another great step!ShatterCoder wrote:Hope this helps. If you get stuck post what you have and I'll see if I can help out
For the recipients, you are using this in the SentTo rule:
item := c.SentTo.Recipients.Item(A_index).AddressEntry
I looked at the Outlook Object Browser (for only the Outlook library, not All Libraries) and then at Rules and RuleConditions, but could not figure out the enumeration of that object for From and Subject. Do you know what your item:= assignment statement would be for the From field? And what it would be for the Subject field?
Thanks again!
-
- Posts: 77
- Joined: 06 Oct 2016, 15:57
Re: How to use Outlook Component Object Model (COM) to export Outlook 365 Rules to readable format
the short answer is that you just change SentTo to From
I've recorded a short video outlining how you can navigate through the object browser to get that info:
https://i.imgur.com/bMurv59.gif
hopefully that will make sense to you and allow you to better extrapolate how to get the other info you need.
Code: Select all
item := c.From.Recipients.Item(A_index).AddressEntry
https://i.imgur.com/bMurv59.gif
hopefully that will make sense to you and allow you to better extrapolate how to get the other info you need.
-
- Posts: 77
- Joined: 06 Oct 2016, 15:57
Re: How to use Outlook Component Object Model (COM) to export Outlook 365 Rules to readable format
I'm not sure how helpful that video is without naration, but I made a second video that also shows the code. I hope it makes it a bit more clear. Sorry it's not the cleanest production, but I think it gets the idea across.
this one is the same basic idea as the first, but is a bit longer and I highlight the code while showing where it came from in the object browser.
https://i.imgur.com/C2PJJ68.gif
this one is the same basic idea as the first, but is a bit longer and I highlight the code while showing where it came from in the object browser.
https://i.imgur.com/C2PJJ68.gif
- JoeWinograd
- Posts: 2200
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: How to use Outlook Component Object Model (COM) to export Outlook 365 Rules to readable format
Works great! That handles the From field. Now for the Subject field. The GIFs are very helpful, although I haven't figured out the answer for Subject. Any thoughts? Thanks!ShatterCoder wrote:the short answer is that you just change SentTo to From
-
- Posts: 77
- Joined: 06 Oct 2016, 15:57
Re: How to use Outlook Component Object Model (COM) to export Outlook 365 Rules to readable format
The subject / BodyOrSubject class's only have a text property outside of the usual stuff like type parent etc. The Object explorer tells us `Text` is of type `variant`. Some digging reveals that this is an array in this case, each member of an "and" or an "or" rule goes in the array. Sadly this is not an array that is numerically indexed Instead it uses the rule string as the index (edit: the value of the array is the RuleConditionType). So you need to use a for loop to iterate over it:
Code: Select all
else if (Condition.ConditionType = 14) ;BodyOrSubject in my example
{
c := Rules.Item(OuterDex).Conditions
for k, v in c.BodyOrSubject.Text
MsgBox, % "condition = " k "`nrule type = " RuleConditionType[v]
}
- JoeWinograd
- Posts: 2200
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: How to use Outlook Component Object Model (COM) to export Outlook 365 Rules to readable format
That's some awesome digging! Works a charm! For my purposes, I changed the check to ConditionType=2 (Subject) and the variable to c.Subject.Text...PERFECT! Thanks so much for your excellent help on this...very much appreciated! Regards, JoeShatterCoder wrote:Some digging reveals...
Who is online
Users browsing this forum: No registered users and 71 guests