Board index » cppbuilder » Outlook contacts vs distribution list

Outlook contacts vs distribution list


2008-02-19 07:00:20 AM
cppbuilder8
This should be easy - but I cannot get it right. In the following code, a
click on the button will grab all the pst folder names and put them into
a listbox. Then, when the user double-clicks on an item in the listbox, the
program enumerates all the contacts within that pst file. However, if the
program comes across a distribution list, it crashes. I know I can check
the class of the item (e.g. contact->Class==olContact), but I can only do
that after the fact - i.e. I've already attempted to get the next item and
assign it to the contact pointer. I cannot figure out how to do it in the
proper order.
On a second note, I have the TOutlookApplication->ConnectKind set to RunningOrNew,
but every time the program exits (even when I don't have my test Distribution
List in the contact list), Outlook closes. Is there a way to keep it running?
Brian
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
OutlookApplication1->Connect();
_NameSpace *objNameSpace=OutlookApplication1->Application->GetNamespace(WideString("MAPI"));
Folders *folders=objNameSpace->Folders;
MAPIFolder *folder=folders->GetFirst();
while(folder)
{ ListBox1->Items->Add(folder->Name);
folder=folders->GetNext();
}
OutlookApplication1->Disconnect();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1DblClick(TObject *Sender)
{ OutlookApplication1->Connect();
_NameSpace *objNameSpace=OutlookApplication1->Application->GetNamespace(WideString("MAPI"));
Folders *folders=objNameSpace->Folders;
MAPIFolder *folder=folders->Item((Variant)ListBox1->ItemIndex+1);
Folders *_folders=folder->Folders;
MAPIFolder *_folder=_folders->GetFirst();
while(_folder)
{ String name=_folder->Name;
if(name=="Contacts")
{ enumerateContacts(_folder);
break;
}
_folder=_folders->GetNext();
}
OutlookApplication1->Disconnect();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::enumerateContacts(MAPIFolder *folder)
{ int i;
ContactItemPtr contact;
ItemsPtr items;
items=folder->get_Items();
for(i=1;i<=items->Count;i++)
{ contact=items->Item(Variant(i));
if(contact->Class==olContact)
Memo1->Lines->Add(contact->FullName);
}
}
//---------------------------------------------------------------------------
 
 

Re:Outlook contacts vs distribution list

Quote
On a second note, I have the TOutlookApplication->ConnectKind set to
RunningOrNew,
but every time the program exits (even when I don't have my test
Distribution List in the contact list), Outlook closes. Is there a way to
keep it running?
I am having this same problem where Outlook closes when I disconnect. Is
there a way to keep Outllok running?
Thanks,
Mike