PDA

View Full Version : Trigger Macro on Category Change/Add



JumblyBug
04-16-2014, 12:49 PM
Hi all,

I'm pretty experienced writing macros using VBA for Excel, but I'm new to writing them for Outlook. I have a few categoried prepared that I use to organize my tasks/emails in Outlook. If I assign one of those categories to an email message, I want a macro to trigger that saves that file to a folder that I designate immediately (I don't want to have to run something periodically). Is this something that can be done?

Thanks in advance!

westconn1
04-17-2014, 05:57 AM
you can do it in the itemsend event, but i do not know that there is any automation on selecting a category, unless the propertychange event of the mailitem is fired

JumblyBug
04-17-2014, 07:34 AM
I was actually able to come up with something, it's not perfect though. The code works, but for some reason only once. It changes the category from "Add to Folder" to "Added to Folder", but I can't get it to save:


Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim Ns As Outlook.NameSpace

Set Ns = Application.GetNamespace("MAPI")
Set Items = Ns.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub Items_ItemChange(ByVal Item As Object)
On Error Resume Next
Dim email As Outlook.MailItem

If TypeOf Item Is Outlook.MailItem And Item.Categories = "Add to Folder" Then
Set email = Item
email.Categories = ""
email.Categories = "Added to Folder"
email.SaveAs "C:\Users\thomas.oconnell\Desktop\Test\" & email.Subject, olMSG
End If

End Sub

westconn1
04-18-2014, 12:51 AM
, but I can't get it to save:what happens? error? wrong result nothing?

probably you should save the email first
then in saveAs you should supply the correct file extension to match the specified file type, like

email.save
email.SaveAs "C:\Users\thomas.oconnell\Desktop\Test\" & email.Subject & ".msg", olMSG

JumblyBug
04-21-2014, 05:20 AM
what happens? error? wrong result nothing?

probably you should save the email first
then in saveAs you should supply the correct file extension to match the specified file type, like

email.save
email.SaveAs "C:\Users\thomas.oconnell\Desktop\Test\" & email.Subject & ".msg", olMSG

This worked great, thanks for your help!