BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Office access

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (ActiveX)
View previous topic :: View next topic  
Author Message
John Franklin
Guest





PostPosted: Fri Mar 23, 2007 12:50 am    Post subject: Office access Reply with quote



Can someone please tell me how to access the Outlook calendar from CBuilder
6? Is there an Active X component that can be imported or a VCL interface?
Where do I find documentation about doing this?

Many thanks if anyone can help

John Franklin
Back to top
Minas
Guest





PostPosted: Fri Mar 23, 2007 6:17 am    Post subject: Re: Office access Reply with quote



Hi ,

you can use Servers Tab components.
Drop an outlook application component in your form and try the
following code.I wrote it in BCB5 but I think is ok also for BCB6

For documentation use the MS Office VBA help which comes with
MS Office suite

*The code is little bit rough just to show you some points*

void __fastcall TForm1::Button1Click(TObject *Sender)
{
OutlookApplication1->Connect();
NameSpacePtr nmsp = OutlookApplication1->GetNamespace(WideString("MAPI"));
nmsp->Logon("","", false, false);

MAPIFolderPtr calendar = nmsp->GetDefaultFolder( olFolderCalendar );
ShowMessage(IntToStr(calendar->Items->Count)); // Shows the Count of
Appointments
IDispatch* itemA = calendar->Items->Item(1); //Get the first Appointment
Item
String txt = "Start: "+VarToStr(getProp(itemA,"Start"))+"\nSubject:
"+VarToStr(getProp(itemA,"Subject"))+"\nBody:
"+VarToStr(getProp(itemA,"Body"));
ShowMessage(txt);
getProp(itemA,"Display"); // Call the Display method

itemA->Release();
calendar->Display(); // dislpays the calendar

}
//---------------------------------------------------------------------------
Variant __fastcall TForm1::getProp(IDispatch* disp,String prop)
{
VARIANT result;
VariantInit(&result);

DISPPARAMS params = {NULL, NULL, 0, 0};

wchar_t *szMember = WideString(prop).c_bstr();
DISPID dispid;
disp->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_USER_DEFAULT,
&dispid);
disp->Invoke( dispid, IID_NULL, LOCALE_USER_DEFAULT,DISPATCH_METHOD |
DISPATCH_PROPERTYGET, &params, &result, 0, NULL);
return result;
}
//---------------------------------------------------------------------------

Best Regards

_minas harokopos

------
"Only the virtue's conquests have certainty" Sofokleous Erephyle

Ο "John Franklin" <john (AT) lesberries (DOT) co.uk> έγραψε στο μήνυμα
news:5sCdnWfRetWHQ5_bnZ2dnUVZ8tGqnZ2d (AT) pipex (DOT) net...
Quote:
Can someone please tell me how to access the Outlook calendar from
CBuilder 6? Is there an Active X component that can be imported or a VCL
interface? Where do I find documentation about doing this?

Many thanks if anyone can help

John Franklin
Back to top
John Franklin
Guest





PostPosted: Sat Mar 24, 2007 1:59 am    Post subject: Re: Office access Reply with quote



Minas,

Thank you very much for your help. I'm making progress but not there yet!

I can read existing appointments using code similar to yours, but I still
cannot save a new appointment.

I'm using the following code:

OutlookApplication1->Connect();
NameSpacePtr nmsp = OutlookApplication1->GetNamespace(WideString("MAPI"));
Variant vProfile="", vPassword="", vDlg=false, vSession=false;
nmsp->Logon(vProfile,vPassword, vDlg, vSession);

Variant vAI = olAppointmentItem;
MAPIFolderPtr calendar = nmsp->GetDefaultFolder( olFolderCalendar );
TAppointmentItem *item = (TAppointmentItem*)calendar->Items->Add
(vAI);//olAppointmentItem);
item->set_Subject(WideString("A new subject").c_bstr());
item->set_Start(Now());
item->set_End(Now());
item->set_Body(WideString("body").c_bstr());
item->Save();

When the new item saves, I get an access violation in an AddRef function.

I thought I'd also try to use the TAppointmentItem control that's part of
the Office97 set of components, thus:

AppointmentItem1->set_Subject(WideString("Test subject").c_bstr());
AppointmentItem1->set_Start(Now());
AppointmentItem1->set_End(Now());
AppointmentItem1->set_Body(WideString("Test body").c_bstr());
AppointmentItem1->Save();

In this case I get a 'class not registered' error. I thought this component
would be registered along with the OutlookApplication component and I'm not
sure what to do to correct.

If you can give me a bit more assistance with this it would be much
appreciated.

John Franklin

"Minas" <no email> wrote in message
news:46032b7a$1 (AT) newsgroups (DOT) borland.com...
Quote:
Hi ,

you can use Servers Tab components.
Drop an outlook application component in your form and try the
following code.I wrote it in BCB5 but I think is ok also for BCB6

For documentation use the MS Office VBA help which comes with
MS Office suite

*The code is little bit rough just to show you some points*

void __fastcall TForm1::Button1Click(TObject *Sender)
{
OutlookApplication1->Connect();
NameSpacePtr nmsp =
OutlookApplication1->GetNamespace(WideString("MAPI"));
nmsp->Logon("","", false, false);

MAPIFolderPtr calendar = nmsp->GetDefaultFolder( olFolderCalendar );
ShowMessage(IntToStr(calendar->Items->Count)); // Shows the Count of
Appointments
IDispatch* itemA = calendar->Items->Item(1); //Get the first Appointment
Item
String txt = "Start: "+VarToStr(getProp(itemA,"Start"))+"\nSubject:
"+VarToStr(getProp(itemA,"Subject"))+"\nBody:
"+VarToStr(getProp(itemA,"Body"));
ShowMessage(txt);
getProp(itemA,"Display"); // Call the Display method

itemA->Release();
calendar->Display(); // dislpays the calendar

}
//---------------------------------------------------------------------------
Variant __fastcall TForm1::getProp(IDispatch* disp,String prop)
{
VARIANT result;
VariantInit(&result);

DISPPARAMS params = {NULL, NULL, 0, 0};

wchar_t *szMember = WideString(prop).c_bstr();
DISPID dispid;
disp->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_USER_DEFAULT,
&dispid);
disp->Invoke( dispid, IID_NULL, LOCALE_USER_DEFAULT,DISPATCH_METHOD |
DISPATCH_PROPERTYGET, &params, &result, 0, NULL);
return result;
}
//---------------------------------------------------------------------------

Best Regards

_minas harokopos

------
"Only the virtue's conquests have certainty" Sofokleous Erephyle

Ο "John Franklin" <john (AT) lesberries (DOT) co.uk> έγραψε στο μήνυμα
news:5sCdnWfRetWHQ5_bnZ2dnUVZ8tGqnZ2d (AT) pipex (DOT) net...
Can someone please tell me how to access the Outlook calendar from
CBuilder 6? Is there an Active X component that can be imported or a VCL
interface? Where do I find documentation about doing this?

Many thanks if anyone can help

John Franklin


Back to top
Minas
Guest





PostPosted: Sat Mar 24, 2007 2:44 pm    Post subject: Re: Office access Reply with quote

"John Franklin" <john (AT) lesberries (DOT) co.uk> wrote

Quote:
TAppointmentItem *item = (TAppointmentItem*)calendar->Items->Add
(vAI);//olAppointmentItem);

the Add method returns a IDispatch pointer so we use the IDispatch
to do the job.

//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
OutlookApplication1->Connect();
NameSpacePtr nmsp = OutlookApplication1->GetNamespace(WideString("MAPI"));
Variant vProfile="", vPassword="", vDlg=false, vSession=false;
nmsp->Logon(vProfile,vPassword, vDlg, vSession);

Variant vAI = olAppointmentItem;
MAPIFolderPtr calendar = nmsp->GetDefaultFolder( olFolderCalendar );
//TAppointmentItem *item = (TAppointmentItem*)calendar->Items->
Add(vAI);//olAppointmentItem);
IDispatch* item = calendar->Items->Add(vAI);

//instead of TDateTime("27/3/2007 18:00") you can get date values
// from a DateTimePicker
// attention in type parameter to be the right

setProp(item,L"Start",TDateTime("27/3/2007 18:00"),VT_DATE);
setProp(item,L"End",TDateTime("27/3/2007 21:00"),VT_DATE);
setProp(item,L"Subject","This is the Subject",VT_BSTR);
setProp(item,L"Body","this is the body",VT_BSTR);

getProp( item,L"Save");
item->Release();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::setProp(IDispatch* disp,WideString prop,Variant
value,int type)
{

VARIANTARG varArg ;
VariantInit(&varArg);

switch (type)
{
case VT_BOOL:
varArg.vt = VT_BOOL ;
varArg.boolVal = value;
break;
case VT_INT:
varArg.vt = VT_INT ;
varArg.intVal = value;
break;
case VT_BSTR:
varArg.vt = VT_BSTR ;
varArg.bstrVal = value;
break;
case VT_DATE:
varArg.vt = VT_DATE ;
varArg.date = value;
break;
}

DISPPARAMS params;
memset(&params, 0, sizeof(DISPPARAMS));
params.cArgs =1;
params.cNamedArgs=0;
params.rgvarg = &varArg;

wchar_t *szMember = prop.c_bstr();
DISPID dispid;
disp->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_USER_DEFAULT,
&dispid);
disp->Invoke( dispid, IID_NULL,
LOCALE_USER_DEFAULT,DISPATCH_PROPERTYPUT, &params, NULL, 0, NULL);
}
//---------------------------------------------------------------------------
Variant __fastcall TForm1::getProp(IDispatch* disp,WideString prop)
{
VARIANT result;
VariantInit(&result);

DISPPARAMS params = {NULL, NULL, 0, 0};
wchar_t *szMember = prop.c_bstr();
DISPID dispid;
disp->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_USER_DEFAULT,
&dispid);
disp->Invoke( dispid, IID_NULL, LOCALE_USER_DEFAULT,DISPATCH_METHOD |
DISPATCH_PROPERTYGET, &params, &result, 0, NULL);
return result;
}
//---------------------------------------------------------------------------

Best Regards
_minas harokopos

------
"Only the virtue's conquests have certainty" Sofokleous Erephyle
Back to top
John Franklin
Guest





PostPosted: Sat Mar 24, 2007 3:33 pm    Post subject: Re: Office access Reply with quote

Minas,

Thank you very much indeed for that. It does what I want it to do, although
I don't completely understand the mechanism, which seems a bit convoluted.
Is there a book or other source you can recommend that explains how to
interface with COM objects such as this?

With regards,

John Franklin

"Minas" <no email> wrote in message news:4604f3c2 (AT) newsgroups (DOT) borland.com...
Quote:
"John Franklin" <john (AT) lesberries (DOT) co.uk> wrote

TAppointmentItem *item = (TAppointmentItem*)calendar->Items->Add
(vAI);//olAppointmentItem);

the Add method returns a IDispatch pointer so we use the IDispatch
to do the job.

//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
OutlookApplication1->Connect();
NameSpacePtr nmsp =
OutlookApplication1->GetNamespace(WideString("MAPI"));
Variant vProfile="", vPassword="", vDlg=false, vSession=false;
nmsp->Logon(vProfile,vPassword, vDlg, vSession);

Variant vAI = olAppointmentItem;
MAPIFolderPtr calendar = nmsp->GetDefaultFolder( olFolderCalendar );
//TAppointmentItem *item = (TAppointmentItem*)calendar->Items-
Add(vAI);//olAppointmentItem);
IDispatch* item = calendar->Items->Add(vAI);

//instead of TDateTime("27/3/2007 18:00") you can get date values
// from a DateTimePicker
// attention in type parameter to be the right

setProp(item,L"Start",TDateTime("27/3/2007 18:00"),VT_DATE);
setProp(item,L"End",TDateTime("27/3/2007 21:00"),VT_DATE);
setProp(item,L"Subject","This is the Subject",VT_BSTR);
setProp(item,L"Body","this is the body",VT_BSTR);

getProp( item,L"Save");
item->Release();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::setProp(IDispatch* disp,WideString prop,Variant
value,int type)
{

VARIANTARG varArg ;
VariantInit(&varArg);

switch (type)
{
case VT_BOOL:
varArg.vt = VT_BOOL ;
varArg.boolVal = value;
break;
case VT_INT:
varArg.vt = VT_INT ;
varArg.intVal = value;
break;
case VT_BSTR:
varArg.vt = VT_BSTR ;
varArg.bstrVal = value;
break;
case VT_DATE:
varArg.vt = VT_DATE ;
varArg.date = value;
break;
}

DISPPARAMS params;
memset(&params, 0, sizeof(DISPPARAMS));
params.cArgs =1;
params.cNamedArgs=0;
params.rgvarg = &varArg;

wchar_t *szMember = prop.c_bstr();
DISPID dispid;
disp->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_USER_DEFAULT,
&dispid);
disp->Invoke( dispid, IID_NULL,
LOCALE_USER_DEFAULT,DISPATCH_PROPERTYPUT, &params, NULL, 0, NULL);
}
//---------------------------------------------------------------------------
Variant __fastcall TForm1::getProp(IDispatch* disp,WideString prop)
{
VARIANT result;
VariantInit(&result);

DISPPARAMS params = {NULL, NULL, 0, 0};
wchar_t *szMember = prop.c_bstr();
DISPID dispid;
disp->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_USER_DEFAULT,
&dispid);
disp->Invoke( dispid, IID_NULL, LOCALE_USER_DEFAULT,DISPATCH_METHOD |
DISPATCH_PROPERTYGET, &params, &result, 0, NULL);
return result;
}
//---------------------------------------------------------------------------

Best Regards
_minas harokopos

------
"Only the virtue's conquests have certainty" Sofokleous Erephyle

Back to top
Minas
Guest





PostPosted: Sat Mar 24, 2007 11:52 pm    Post subject: Re: Office access Reply with quote

Hi John ,

the whole mechanism based on IDispatch interface which is our *door*
to access properties and methods of an automation object.Two important
functions are GetIDsOfNames and Invoke.

To get and set the values of properties or to call a method of an Automation
object we use Invoke(int DispID, const GUID &IID, int LocaleID...).

The first parameter is an integer (DISPID) which identifies the
member(method
or property of the auto object) we want to invoke. Here is where we need
GetIDsOfNames
to get that member id by just providing the name(like
"Start","Subject","Body" or "Display","Print")
of property or method of the autom.object.

These dispatch IDs that identify members(properties or methods) are defined
by the implementor(in our case MS) of the object. We just use GetIDsOfNames
to get
them for use in Invoke function.

If we want to pass parameters in a member( method or property) we use
DISPPARAMS
structure in parameter-list of Invoke function. If we expect some
result(like in getProp to read an appointment)
we get it in pVarResult parameter of Invoke function.
Important is the parameter wFlags (see in help)

There are many Books about COM programming . I didn't have any because
are orientated (mostly) in MS Visual Studio if I am not wrong - I didn't
search a lot to say the truth Smile .
I have BCB5 PRO which has good documentation .Take a look at
" Developing COM-based applications" .
Also take a look in (BCB help) Microsoft Win32 Programmer's Reference about
IDispatch Interface.
Very helpful for me was C++Builder 5 Developer's Guide with an example in
implementing COM Server/Client

Also about Dispatch Interface
http://msdn2.microsoft.com/en-us/library/ms221608.aspx

Best Regards
_minas harokopos

------
"Only the virtue's conquests have certainty" Sofokleous Erephyle

"John Franklin" <john (AT) lesberries (DOT) co.uk> wrote
Quote:

Thank you very much indeed for that. It does what I want it to do,
although I don't completely understand the mechanism, which seems a bit
convoluted. Is there a book or other source you can recommend that
explains how to interface with COM objects such as this?

With regards,

John Franklin
Back to top
Minas
Guest





PostPosted: Sat Mar 24, 2007 11:56 pm    Post subject: Re: Office access Reply with quote

Important ,also, is the VBA help when accessing MS Office automation objects

Best Regards
_minas harokopos

------
"Only the virtue's conquests have certainty" Sofokleous Erephyle
Back to top
John Franklin
Guest





PostPosted: Sun Mar 25, 2007 2:10 am    Post subject: Re: Office access Reply with quote

Thanks a million, Minas. I'm very grateful for all your help.

Best wishes,

John
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (ActiveX) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.