• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

Search Top Level Folders in Outlook

I am using the Microsoft Outlook 10.0 object  library to access Outlook. For some reason I am able to use the Folder.Find method on any sub-folder and provided there is mail that meets the search criteria, this mail is returned by the method and can be manipulated.

If I initialise a folder object to the Inbox, and attempt to use the .Find method, I never get a result. Does the .Find method work on the Inbox? Is there something particular to the Inbox that needs to be done in order for .Find to work?

My code looks similar to what follows:

set fldInbox = NS.GetDefaultFolder(olFolderInbox)
set MSG = fldInbox.Items.Find(sCriteria)

       '''  OR

set MSG = fldInbox.Find(sCriteria)

MSG.Display    '''err is Object Variable not set '91'

Thanks All.
1 Solution
Hmm, I wonder. I use library version 9.0 and the following code to search the inbox:

    Dim app As Outlook.Application
    Dim ns As NameSpace
    Set app = GetObject(, "Outlook.Application")
    Set ns = app.GetNamespace("MAPI")
    Dim fld As MAPIFolder, itm As MailItem
    Set fld = ns.GetDefaultFolder(olFolderInbox)
    Set itm = fld.Items.Find("[From] = ""Experts Exchange""")
    Debug.Print itm.Subject

You may want to cut and paste it and compare it to your code or temporarily replace it to see if my code works at your machine.

Note that the message "Object variable or With block variable not set" (91) means that the search executed without an error, but did not have any results. The above one works for my inbox because I do have mail from Experts Exchange (and you probable have some too).

Btw, maybe you rather use CreateObject instead of GetObject, but that choice you've probably already made.

If this all doesn't help, please give a full dump of your code, maybe there's something missing, or maybe you simply overlook some tiny detail.

RobDillonAuthor Commented:
Thanx Abel,

Since posting I've realised there is an issue omitted from my question (Apologies). The message field I wish to search by is a user property. This does not explain why my searches work on the subfolders but not on the Inbox?

I have added a column in all my outlook folders for the user property in question. The only messages that are found by the .Find method called on the inbox are 2 messages whose forms I edited to have a text box bound to my user property. If necessary I can replace my user's default mail forms on installation but I feel this is probably not necessary since I am able to search by the user property without doing this where the sub folders are concerned.

Outlook gurus?
Maybe you should place a reference to this question in the MS Outlook area. Or delete this q. and repost it there.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

RobDillonAuthor Commented:
Thanx Abel,
I will take your advice, but against my better judgement and all the rules of ettiquette I will commit the ultimate sin and .... Double Post!
Hi RobDillon,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Refund points and save as a 0-pt PAQ.

RobDillon, Please DO NOT accept this comment as an answer.
EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
DanRollins -- EE database cleanup volunteer
Moving to the paq

Experts Exchange Moderator
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now