Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook Automation - Help locating oFolder without looping through all Folders

Status
Not open for further replies.

MikeCoop84

Programmer
Apr 7, 2015
3
0
0
US
I was wondering if anyone knows a way of getting to Outlook's Folder object directly, without looping through all of the possible folder names like I've done in the example below.

My code works well, but, we have hundreds of Public Folders in the company. If the Mailbox you're looking for is located after the Public Folders, or you're looking for a Public Folder towards the bottom of the list, it can take several minutes.

oOutlook = CREATEOBJECT("Outlook.Application")
oNameSpace = oOutlook.GetNameSpace("MAPI")
oFolders = oNameSpace.Folders
FOR EACH oFolder IN oFolders
lcName = ALLTRIM(oFolder.NAME)​
IF 'Search String' $ lcName​
loTargetFolder = oFolder​
EXIT​
ELSE​
IF oFolder.Folders.COUNT > 0​
*** drill down to sub-folders using this same method​
ENDIF​
ENDIF​
*** STILL HAVE TO EXIT OUTTER LOOP IF FOUND​
IF VARTYPE(loTargetFolder) = 'O'​
EXIT​
ENDIF​
ENDFOR

TIA,
Mike
 
Pretty sure you can just put a direct reference inside TRY-CATCH (in case it doesn't exist):

Code:
TRY
  oFolder = oFolders["Search String"]
CATCH
  oFolder = .null.
ENDTRY

However, that won't drill down. So, if you don't find it this way, you'll have to find folders that contain other folders and try the same thing on them (recursively, until you get there).

Tamar
 
Thanks Tamar,

I've had your book for approx. 15 years now. I couldn't get the direct reference to work, but, I did find another method that I think I'm going to use.

It takes my original program to determine what the EntryID is for the Inbox you're looking for, but, once you have that, it's really fast.

oOutlook = CREATEOBJECT("Outlook.Application")
*** A MAPI store provider assigns a unique ID string when an item is created in its store.
lcEntryID = [0000000082FE351AA8F824488EF47932650772520100EA3F09E331105045BEE09923C85D8D2600001B7FD7830000]
loInbox = oOutlook.Session.GetFolderFromID(lcEntryID)
IF VARTYPE(loInbox) = 'O'
*** LOOK FOR EMAILS​
FOR EACH oeMail IN loInbox.Items​
*** Get Email Properties​
lcTSubject = oeMail.Subject​
lcSenderEmailAddress = oeMail.SenderEmailAddress && X400​
lcSenderName = oeMail.SenderName​
?lcSenderName+' -- '+lcSenderEmailAddress​
?SPACE(10) + lcTSubject​
ENDFOR​
ENDIF
 
For sure, using the ID is going to be faster than anything that has to search.

Tamar

P.S. Glad the book has been helpful.
 
One more thing I thought I should mention. I've found that it's better to use both the EntryID and StoreID with GetFolderFromID().

EntryID alone worked fine when the Outlook Application was up and running on the computer. When Outlook was closed, I received a strange OLE error.

If you use both parameters, Outlook does NOT have to be open.

Once you find the folders you're looking for, you can get the parameters by calling:

lcEntryID = loInbox.EntryID​
lcStoreID = loInbox.StoreID​

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top