PDA

View Full Version : [SOLVED:] ItemChange with Categories



Gandolf_Red
08-26-2015, 06:49 AM
Hello,

I write a lot of VBA in excel, but am newer to using it within outlook. I am trying to find a way to send a reply to the person who sent an e-mail when I change the category. Here is what I have, but it does not seem to be working. Any insight would be appreciated!

Private Sub Application_Startup()
Dim Msg As Outlook.MailItem
Dim Ns as Outlook.NameSpace
Set Ns = Application.GetNamespace("MAPI")
Set Items = Ns.GetDefaultFolder(olFolderInbox).Items
Dim oRespond As Outlook.MailItem
End Sub

Private Sub Items_ItemChange(ByVal Item As Object)
If TypeOf Item Is Outlook.MailItem AND Item.Categories = "xyz" Then
Set oRespond = Application.CreateItemFromTemplate("file path here")
With oRespond
.Recipients.Add Item.SenderEmailAddress
.Subject = "Re: xyz - " & Item.Subject
.HTMLBody = oRespond.HTMLBody & vbCrLf & "--- original message attached ---" & vbCrLf & Item.HTMLBody & vbCrLf
.Attachments.Add Item
.Send
End With
Set oRespond = Nothing

ElseIf 'My macro repeats the above If statement with different category
End If
End Sub


Thank you

Gandolf_Red
08-26-2015, 10:09 AM
To add some more detail...

There is no error message that I am getting, it just simply does not trigger the macro when I change the category on a mail item in my inbox to any of the categories defined in my if statements.

skatonni
08-26-2015, 01:59 PM
Missing


Private WithEvents Items As Items

https://msdn.microsoft.com/en-us/library/office/ff865866(v=office.14).aspx


Option Explicit

Private WithEvents Items As Items

Private Sub Application_Startup()
Dim Msg As Outlook.MailItem
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)

Dim oRespond As Outlook.MailItem

If TypeOf Item Is Outlook.MailItem And Item.Categories = "xyz" Then
Set oRespond = Application.CreateItemFromTemplate("file path here")
With oRespond
.Recipients.Add Item.SenderEmailAddress
.Subject = "Re: xyz - " & Item.Subject
.HTMLBody = oRespond.HTMLBody & vbCrLf & "--- original message attached ---" & vbCrLf & Item.HTMLBody & vbCrLf
.Attachments.Add Item
.Display
'.send

End With

Set oRespond = Nothing

Else

MsgBox "My macro repeats the above If statement with different category"

End If

End Sub

Gandolf_Red
08-26-2015, 07:51 PM
Thank you!!!! That worked perfectly!