VBA Macro to run rules for Multiple Email accounts - Microsoft Community


i modified macro described at outlook.com for running rules 1 @ time default store. changes involved bit more error testing, using application.session.accounts iterate through each email account (based on account.deliverystore). here code:

sub runallinboxrules()
    dim osession namespace
    dim eachacct account
    dim st outlook.store
    dim myrules outlook.rules
    dim rl outlook.rule
    dim count integer
    dim rulelist string

    set osession = application.session
    if osession.exchangeconnectionmode = olcachedoffline or _
        osession.exchangeconnectionmode = oloffline then
        ' offline, no can run rules
        msgbox "outlook offline; cannot run rules", vbokonly + vbexclamation, "can't run rules"
        goto exit_rule
    end if

    rulelist = ""   ' initialize info string
    ' online. each account exchange or imap, try run rules
    acct = 1 osession.accounts.count
        ' iterate rules
        set eachacct = osession.accounts.item(acct)
        if acct > 1 then
            ' blank line between accounts (after first account)
            rulelist = rulelist & vbcrlf
        end if
        if eachacct.accounttype = olexchange or eachacct.accounttype = olimap then
            ' there rules process
            rulelist = rulelist & "processing rules account: " & eachacct.displayname & vbcrlf & "===========================" & vbcrlf
            ' store account
            set st = eachacct.deliverystore

            ' iterate through rules
            set myrules = st.getrules
            count = 1 myrules.count
                on error goto badrule_err
                ' determine if it’s inbox rule
                set rl = myrules.item(count)
                if rl.ruletype = olrulereceive , rl.enabled then
                    ' if so, run it. see https://msdn.microsoft.com/en-us/library/office/ff864433.aspx args
                    ' supplied (shows defaults):
                    ' rl.execute showprogress:=false, folder:=inbox, includesubfolders:=false,
                    '                          ruleexecuteoption:=olruleexecuteoption.olruleexecuteallmessages
                    rl.execute ruleexecuteoption:=olruleexecuteoption.olruleexecuteallmessages
                    rulelist = rulelist & rl.name & vbcrlf
                elseif rl.ruletype = olrulereceive then
                    rulelist = rulelist & "[disabled - not run] " & rl.name & vbcrlf
                end if  ' sort of olrulesend rule...
                goto next_rule

badrule_err:
                ' went sideways processing rule
                rulelist = rulelist & "error processing rule item # " & count & ", error info: " & err.number & vbcrlf
                resume next_rule
next_rule:
            next ' rule
        ' end of if block on account type
        else
            ' account type doesn't support rules
            rulelist = rulelist & "no rules account: " & eachacct.displayname & vbcrlf & "===========================" & vbcrlf
        end if

    next ' account

    ' tell user did (uncomment next 2 lines show)
    ' rulelist = "these rules executed against inbox: " & vbcrlf & rulelist
    ' msgbox rulelist, vbinformation, "macro: runallinboxrules"
exit_rule:
    ' free memory
    set rl = nothing
    set st = nothing
    set eachacct = nothing
    set myrules = nothing
    set osession = nothing
end sub

my configuration has (default) exchange mailbox, few imap accounts , 2 pop accounts. i've found works great on exchange account seems no-op imap accounts (pop accounts skipped code). when run debugger, can see each account being selected (i.e. in for acct loop, account names expected), see rules loaded (set nyrules = st.getrules indeed load expected rule set), , apparently executed (with statement: rl.execute ruleexecuteoption:=olruleexecuteoption.olruleexecuteallmessages).

however in test i've run more once, leave read messages in 1 imap account's inbox (so usual rules, run on unread messages, leave them alone) , yet rule execution, when run automatically outlook, moves messages pst folder, nothing.

this fresh install of office 2016 (32-bit) on windows 7 x64 sp1. profile created scratch. rules re-added after profile creation.

suggestions appreciated... thanks!

- dennis

hi dennis,

to better assist concern, highly recommend post concern in technet forum instead. forum members in technet more familiar concern , able answer question.

let know how goes.



Office / Outlook / Microsoft Office Programming / Office 2016



Comments

Popular posts from this blog

message deleted by transport agent, event: LED=550 4.3.2 - Microsoft Community

Outlook conversion problem - Microsoft Community

Outlook 2016 - Insert attachment to Meeting Invite You Do Not Own - Microsoft Community