Consulting

Results 1 to 4 of 4

Thread: ItemChange with Categories

  1. #1

    ItemChange with Categories

    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

  2. #2
    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.

  3. #3
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Missing
    Private WithEvents Items As Items
    https://msdn.microsoft.com/en-us/lib...ffice.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
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  4. #4
    Thank you!!!! That worked perfectly!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •