In my question, In MS Outlook VBA, why is looping through a table five times faster than looping through the folder?, in the accepted answer by Dmitry Streblechenko, he made a comment that to efficiently include the count of attachments in the output from a table, I should first test for PR_HASATTACH
being true, and only if not, do the slow access of MailItem.attachments.count
. I implemented that suggestion with the following code:
nAttachments = iif(not(oTableRow("http://schemas.microsoft.com/mapi/proptag/0x0E1B000B")), 0, _
Application.GetNamespace("MAPI").GetItemFromID(oTableRow("EntryID")).Attachments.Count)
There are two problems with that. One is that it did not improve the efficiency because VBA's iif()
stupidly evaluates both conditions. But the worse problem is that the value of the above code is always zero because
not(oTableRow("http://schemas.microsoft.com/mapi/proptag/0x0E1B000B"))
is mysteriously always true.
Why is that? And how do I fix the code? (I am posting this question in order to post the answer that I've found.)