BUG - UnReadItemCount and Object Variable References

  • Thread starter Anthony Marchesini
  • Start date
A

Anthony Marchesini

I believe that I've uncovered some very odd/buggy behavior with the Outlook
Object model. The first thing I've noticed is that Outlook returns a
different object variable reference each time certain objects are accessed.
For instance, consider the following code run from ThisOutlookSession:

Dim F1 As MAPIFolder, F2 As MAPIFolder
Set F1 = Session.GetDefaultFolder(olFolderInbox) 'F1 and F2 should both
Set F2 = Session.GetDefaultFolder(olFolderInbox) 'point to the Inbox
Debug.Print F1.EntryID = F2.EntryID 'Prints True.
Debug.Print F1 Is F2 'Prints False!

While the EntryIDs are equal, the object variable references are not. So,
the lesson is to use EntryID and not the Is operator to check for object
equivalency. If anyone has seen this documented by MS, I'd appreciate a
link.

OK, so the objects are different but the underlying properties should be the
same, right? Well, if property in question is UnReadItemCount, then the
answer is that it depends on how you got hold of the MAPIFolder. Another
example to demonstrate:

Public Sub Test()
Static F1 As MAPIFolder, F2 As MAPIFolder

If F1 Is Nothing Then
'Cache two different references to the Inbox
Set F1 = Session.GetDefaultFolder(olFolderInbox)
Set F2 = Session.Folders("Personal Folders").Folders("Inbox")
End If

Debug.Print
Debug.Print "Variable", "Folder Name", "UnReadItems", "TotalItems"
Debug.Print "F1", F1.Name, F1.UnReadItemCount, F1.Items.Count
Debug.Print "F2", F2.Name, F2.UnReadItemCount, F2.Items.Count
Debug.Print "*", Session.GetDefaultFolder(olFolderInbox).Name, _
Session.GetDefaultFolder(olFolderInbox).UnReadItemCount, _
Session.GetDefaultFolder(olFolderInbox).Items.Count
End Sub

The first time I run this on my machine I get this output:

Variable Folder Name UnReadItems TotalItems
F1 Inbox 463 2116
F2 Inbox 463 2116
* Inbox 463 2116

Now, I go to my inbox and delete one read message (which should decrement
the TotalItems but not UnReadItemsCount) and mark one read message as
unread (which should increment the UnReadItemsCount and leave TotalItems
unchanged). Now run procedure Test again and this is the output:

Variable Folder Name UnReadItems TotalItems
F1 Inbox 464 2115
F2 Inbox 463 2115
* Inbox 464 2115

Notice that F2's UnReadItemCount doesn't match the others even though they
are all pointing to the same Inbox folder! Something about grabbing a
reference to the Inbox via the Folders collection (as opposed to via
GetDefaultFolder) results in a folder object where UnReadItemCount never
gets updated anymore. Looks like a bug to me...

While I don't know if it makes any difference, I'm running Outlook 2003
without an Exchange Account.

- Anthony
 
A

Anthony Marchesini

Sorry for the accidental cross-post. This thread was originally intended
for microsoft.public.office.developer.outlook.vba where I feel it will be
more relavant. Please post all replies on that newsgroup instead.

- Anthony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top