vba - Create Rule to Move Email By Sender Domain -
i able use macro below create rule send email selected sender address designated folder.
this works fine. want create rule send email domain (regardless of sender) folder.
here code using.
dim colrules outlook.rules dim orule outlook.rule dim colruleactions outlook.ruleactions dim omoveruleaction outlook.moveorcopyruleaction dim ofromcondition outlook.toorfromrulecondition dim orulecondition outlook.addressrulecondition dim oexceptsubject outlook.textrulecondition dim oinbox outlook.folder dim omovetarget outlook.folder 'specify target folder rule move action set oinbox = application.session.getdefaultfolder(olfolderinbox) 'assume target folder exists set omovetarget = application.session.folders("myinbox").folders("folders").folders("reference").folders("vendor marketing") 'get rules session.defaultstore object set colrules = application.session.defaultstore.getrules() dim ssender string each objitem in application.activeexplorer.selection if objitem.class = olmail ssender = objitem.senderemailaddress end if next dim domain() string domain = split(ssender, "@") dim ddomain string ddomain = "@" + domain(1) 'create rule adding receive rule rules collection if msgbox("do want create rule " + ssender + "?", vbokcancel) = vbok set orule = colrules.create(ssender, olrulereceive) 'specify condition in toorfromrulecondition object set ofromcondition = orule.conditions.from ofromcondition .enabled = true .recipients.add (ssender) .recipients.resolveall end 'specify action in moveorcopyruleaction object 'action move message target folder set omoveruleaction = orule.actions.movetofolder omoveruleaction .enabled = true .folder = omovetarget end 'update server , display progress dialog colrules.save orule.execute showprogress:=true end if
ok, after more diggings/trial , error. found solution. main thing see type "addressrulecondition" , property want modify not "text", "address"
dim colrules outlook.rules dim orule outlook.rule dim colruleactions outlook.ruleactions dim omoveruleaction outlook.moveorcopyruleaction dim ofromcondition outlook.toorfromrulecondition dim orulecondition outlook.addressrulecondition <--------here dim oexceptsubject outlook.textrulecondition dim oinbox outlook.folder dim omovetarget outlook.folder 'specify target folder rule move action set oinbox = application.session.getdefaultfolder(olfolderinbox) 'assume target folder exists set omovetarget = application.session.folders("myinbox").folders("folders").folders("reference").folders("vendor marketing") 'get rules session.defaultstore object set colrules = application.session.defaultstore.getrules() dim ssender string each objitem in application.activeexplorer.selection if objitem.class = olmail ssender = objitem.senderemailaddress end if next dim domain() string domain = split(ssender, "@") dim ddomain string ddomain = "@" + domain(1) 'create rule adding receive rule rules collection if msgbox("do want create rule " + ddomain + "?", vbokcancel) = vbok set orule = colrules.create(ddomain, olrulereceive) 'specify condition in toorfromrulecondition object 'set ofromcondition = orule.conditions.from 'with ofromcondition '.enabled = true '.recipients.add (ssender) '.recipients.resolveall 'end set orulecondition = orule.conditions.senderaddress orulecondition .enabled = true .address = array(ddomain) <--------here end 'specify action in moveorcopyruleaction object 'action move message target folder set omoveruleaction = orule.actions.movetofolder omoveruleaction .enabled = true .folder = omovetarget end 'update server , display progress dialog colrules.save orule.execute showprogress:=true end if
Comments
Post a Comment