0

I am trying to figure out how to keep track of the number of mail items going into a folder by putting a listener on that folder but it isn't working right. This is what I have but for some reason the selected amount isn't correct when I drag multiple emails to that folder. Basically what I am trying to do with the folder is change all the names of the mail items that are dragged into it to the same name. So I need to be able to have a way to reference those items that are being dragged in. I thought I could do this by using Selection but I'm not sure. Any ideas?? Thank you! Here's my code:

Private WithEvents MatchTicketNumberItems As Outlook.Items

Private Sub Application_Startup()
  Dim olApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Set olApp = Outlook.Application
  Set objNS = olApp.GetNamespace("MAPI")

  'Specify folders that will have listeners on them
  Set MatchTicketNumberItems = objNS.GetDefaultFolder(olFolderInbox).Parent.Folders("MatchTicketNumber").Items
End Sub

Private Sub MatchTicketNumberItems_ItemAdd(ByVal item As Object)
   Dim selected As Integer
   Dim objSelection As Outlook.Selection
   Set objSelection = Application.ActiveExplorer.Selection
   selected = objSelection.count

   'Do stuff
End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
gcc
  • 283
  • 1
  • 3
  • 14

2 Answers2

0

Keep in mind that MAPI events were for the UI purposes only, and can be (and are) skipped under heavy loads.

That being said, can you be more specific? How many items do you drag and how many times does the event handler fire?

Also, why are you working with Explorer.Selection? If an item is created directly, the current selection will be irrelevant.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • For example: I have 3 messages in my inbox. One of them has a subject that I want the other two to have. So I want to move them all together to the "MatchTicketNumber" folder, where that folder will loop through them (For Each obj In objSelection etc...) , find the item with the certain subject I'm looking for, and then copy that subject to the others. Then once it has done that, I want all the items to be moved back to my main inbox. Weird, I know. But I need some way to reference all those files, which I thought I could do by looping through the Selection. What do you mean by irrelevant? – gcc Aug 29 '13 at 21:54
  • So what is the exact problem? Are you saying you have 3 items selected, but Selection count returns a different number? Keep in mind that when ItemAdd event fires, the item is already in the new folder, so it is gone from the selection unless you are using Copy (Ctrl+drag). – Dmitry Streblechenko Aug 30 '13 at 03:47
  • It looks like all I needed to do was hold Ctrl like you said so that the items would stay in the selection. Thank you very much sir! – gcc Aug 30 '13 at 15:50
  • Well, you are lucky if you can force the end user to always do that :-) – Dmitry Streblechenko Aug 30 '13 at 17:54
0

The earlier answer addresses the question but you could try this way that automates the move to the the MatchTicketNumber folder.

Private Sub MatchTicketNumberItems_ItemAdd(ByVal item As Object)
   Dim objNS As Outlook.Namespace
   Dim i As Long
   ' Do Stuff
   item.Save    
End Sub


Sub Process_Selection_Multiple_MatchTicketNumber()     
    Dim objNS As Outlook.Namespace
    Dim targetFolder As MAPIFolder
    Dim selectionIndex As Long
    Dim itm As Object

    Set objNS = Application.GetNamespace("MAPI")
    Set targetFolder = objNS.GetDefaultFolder(olFolderInbox).Parent.Folders("MatchTicketNumber")

    For selectionIndex = 1 To ActiveExplorer.Selection.count
        Set itm = ActiveExplorer.Selection(selectionIndex)
       itm.Move targetFolder
    Next
End Sub
niton
  • 8,771
  • 21
  • 32
  • 52