TOutlookApplication causes Outlook to close


2004-02-28 02:35:28 AM
cppbuilder53
I'm trying to add contact names with full name and email address to a List
and it worked just fine (even with the outlook's alert about allowing or not
an external program to accest the address book) until I tried to run my
program with the Outlook Application running... for some reason after
allowing access (in the outlook alert), Outlook closes.
This is, ofcourse an undesirable side effect, so my question is:
does anyone have any idea on how to avoid Outlook from closing when my
program connects to Outlook's address book...
Any help will be appreciated...
Here is the code I'm working on:
/*
OutlookApplication1 properties are set as follows:
AutoConnect=false
AutoQuit=false
ConnectKind=RunningOrNew
*/
//Constructor
__fastcall TAddContactForm::TAddContactForm(TComponent* AOwner)
: TForm(AOwner)
{
//ns is a form's member variable: _NameSpacePtr ns;
ns= OutlookApplication1->GetNamespace(WideString("MAPI"));
}
...
void __fastcall TAddContactForm::FormShow(TObject *Sender)
{
MAPIFolderPtr contacts;
ContactItemPtr contactItem;
AnsiString temp;
SrcList->Items->Clear();
OutlookApplication1->Connect();
contacts = ns->GetDefaultFolder(olFolderContacts);
for(int i=1;i<contacts->Items->Count;i++)
{
contactItem=contacts->Items->Item(i);
if(contactItem->Email1Address!=NULL && contactItem->FullName!=NULL )
{
temp=contactItem->FullName;
temp=temp+" <";
temp=temp+contactItem->Email1Address;
temp=temp+">";
}
SrcList->Items->Add(temp);
}
OutlookApplication1->Disconnect();
}