I have a collection that I use to display a Map String -> MailItem . I fill in the map, and when I find a duplicate, I want to read the item in the collection.
It seems so easy, but I spent more than an hour trying to figure out why I cannot assign the Collection element to a local variable. (see PROBLEM in the code below)
oMailOther = cMails.Item(cMailKey) "An object variable or with a given block variable
Set oMailOther = cMails.Item(cMailKey) "Required Object"
Another form of cMails(cMailKey) gives the same error. Dim's movement around doesn't matter. cMails should be available as it was used earlier in the method. Pay attention to the line Debug.Print immediately before this statement , which works . What am I missing?
Option Explicit Option Compare Text Public cMails As Collection Public Sub GetOutlookAttachments() Set cMails = New Collection Dim oStore As Store For Each oStore In Session.Stores If oStore.DisplayName = "Outlook Data File" Then ProcessFolder oStore.GetRootFolder() End If Next End Sub Private Sub ProcessFolder(oFolder As Folder) Debug.Print oFolder.FolderPath ProcessItems oFolder.Items Dim oSubFolder As Folder For Each oSubFolder In oFolder.Folders ProcessFolder oSubFolder ' recurse Next End Sub Private Sub ProcessItems(oItems As Items) Dim oItem As Object For Each oItem In oItems DoEvents If TypeOf oItem Is MailItem Then Dim oMail As MailItem Set oMail = oItem Dim cMailKey As String cMailKey = oMail.ConversationID & "-" & oMail.ConversationIndex If Not Contains(cMails, cMailKey) Then cMails.Add oMail.Subject, cMailKey Else Debug.Print cMails.Item(cMailKey) Dim oMailOther As MailItem PROBLEM oMailOther = cMails.Item(cMailKey) Debug.Print cMailKey & ": " & oMailOther.Subject End If ElseIf TypeOf oItem Is MeetingItem Then ' ignore Else Debug.Print "oItem Is a " & TypeName(oItem) End If Next oItem End Sub Public Function Contains(col As Collection, key As Variant) As Boolean Dim obj As Variant On Error GoTo err Contains = True obj = col(key) Exit Function err: Contains = False End Function
I also tried to replicate similar calls to Add and Item elsewhere, but it works .
Public Sub Test() Set cMails = New Collection Dim cMailKey As String cMailKey = "hello" cMails.Add Session.Stores.Item(1), cMailKey Debug.Print cMails(cMailKey) Dim oStore As Store Set oStore = cMails(cMailKey) Debug.Print oStore.DisplayName End Sub
source share