views:

100

answers:

3

SOLUTION BELOW

I've been looking all over the net to find a solution for this, but it seems quite hard to get an answer for this in Delphi...

Skip this if you're familiar with Outlook

Some explanation before: The Contacts Folder in Outlook is organized like a foldertree in Windows. The Contacts are stored in the Contacts Folder itself or within subfolders.

My Code does add Contacts from an external Database into the Outlook contacts Database. To prevent double entries the programm is supposed to check all contacts and see if it can find an 'older' version of the contact entry and update it, or if not, create a new one.
Therefore I wrote a recursion which loops through the folders and checks the contacts.

Within a folder you can get the subfolder by (besides Next, Previous and Last)

Contacts:= Contacts.Folders.Getfirst

//The now selected Folder is the first subfolder within the previous selected one

If I am trying to get any property of this Subfolder like 'Items.Count' or anything else, an error occurs because this folder doesn't exist.
Therefore I want to check if the Folder exists or not, and skip to loop through this subfolder because otherwise the loop would break here and the program stops.

Skip until here if you're familiar with Outlook workings
THE PROBLEM:

In Debugger this Contacts/Folder Variable (an OleVariant, Pointer to the now selected Folder) contains values similar to this: '$0074974C'.
If there is no subfolder this value returns '$00000000'. This seems to be a pointer.

How should I check if a folder exists or not?

const
  olFolderContacts = $0000000A;
var
  outlook, NameSpace, Contact, ContactsRoot, Contacts: OleVariant;

begin
Outlook := CreateOleObject('Outlook.Application');
NameSpace := Outlook.GetNameSpace('MAPI');
ContactsRoot := NameSpace.GetDefaultFolder(olFolderContacts);
Contacts:= ContactsRoot;
//We're now in the Contacts Folder
Contacts:= Contacts.folders.getfirst;
//First Subfolder

What didn't work: Check if

Contacts = '$00000000' (As string)

Contacts = '$00000000' (As OleVariant)

var
val:TVarRec;
code:
val:=Contacts;
string(Contacts.VWideChar) = '$00000000'

var
vntNothing: OLEVariant;
code:
TVarData(vntNothing).VType := varDispatch;
TVarData(vntNothing).VDispatch := Nil;
Contacts = vntNothing

Contacts = unassigned
...
...

In VBA this problem has a simple solution

if Contacts = Nothing

But there is no 'Nothing' in Delphi...

Ideas?

A: 
var  
x: string;  

in code:

x:= format('%p%',[Pointer(TVarData(contacts).VDispatch)]);
if x = '00000000' then  
   'New Contact'  
else  
   'open folder and search within this one'

Co-worker had the solution.. Thanks for your time :)

fps
Why the formatting to string? If that works, why not just use `Pointer(TVarData(Contacts).VDispatch) = nil`?
The_Fox
Think its a leftover of my previous atempts to find a solution, your solution is a bit shorter but working as well, thanks :)
fps
A: 

You could try this:

if IUnknown(Contacts) = nil then
  //
The_Fox
Your solution works as well, thanks for your help.
fps
+2  A: 

You could first check the count on the Folders collection:

if Contacts.Folders.Count = 0 then

or

Contacts := Contacts.Folders.GetFirst;
if VarIsClear(Contacts) then
TOndrej