delphioutlookexchange-serverpublic-folders

Detect if public folder (of exchange) exists in Outlook


With the following Code-Block you select the public folder which is set in Outlook:

const
  olPublicContactsFolder = $00000012; //constant for the public folder
begin
Outlook := CreateOleObject('Outlook.Application');
// Get name space
NameSpace := Outlook.GetNameSpace('MAPI');
// Get root public folder
ContactsRoot := NameSpace.GetDefaultFolder(olPublicContactsFolder); //<-- Error
Contacts:= Contactsroot;

An error occurs on the marked line if there is no public folder in Outlook (no public folder set in Outlook, no Exchange Server).

The question is how to avoid that error by previously detecting if there is a public folder set or not.

Catching the error with a try...finally/except block didn't work as the exception is caused externally by the Microsoft API (EOleException).

I can't think of another way to detect if the folder exists as the line causing the error is essential in selecting the public folder and therefore getting properties of it.

greetings


Solution

  • Why wouldn't try/except work? Delphi catches EOleSysError exceptions just fine. And the exception is raised by the Delphi RTL, not Outlook - all IDispatch-friendly libraries return an error code, which the RTL converts to an OLE exception after requesting the description using IErrorInfo.