Art Delphi Automation History Home Politics Email me Default Colours Printable Colours
There are some demos using the server components in the Demos directory. The Delphi help files give an example of using the Connect method with Excel. And that's about it.
Well, no, actually - things aren't as bad as all that. This site has a bit specifically about the components, for one thing; but more importantly, the help that is available for D3 and D4 automation is still very relevant in Delphi 5. The components themselves are only the thinnest of wrappers around the underlying interfaces, so you can treat them almost exactly as you would an interface variable in D4-style code. For example, if you're automating Outlook, and you've seen some code like this:
var
MI: MailItem;
...
MI.Recipients.Add('DaleFuller@inprise.com');
MI.Subject := 'My need for Delphi 5 Enterprise';
MI.Body := 'Send me D5 Enterprise now, or the kid gets it';
MI.Send;
you can just substitute a MailItem component for that MailItem variable and
the code will work fine. A minor advantage of the components is that they overload
some methods, so that you don't have to supply as many EmptyParam and LCID parameters -
but the code will work fine if you do. The only major differences are in starting
up the application, connecting the components to the interfaces you want, and
handling the application's events.
This is easy. Pop the application component on a form, and either set its AutoConnect method to True, or call its Connect method when you're ready:
WordApplication1.Connect;
But I haven't got an application component!
The components you get depend on the declarations in the type library created by the application's programmers. In the case of Access, for example, Microsoft declared the Application object as hidden, so it can't be created as a component. However you can still use the interface, by calling CoApplication.Create in the traditional D4 manner (see the Access page for code). And once you've got the application interface, you can connect the other components to it in the normal way - see Connecting the components.
Can I use a document component instead of the application component?
Possibly, it depends on the application. You should have no trouble with Word, for example; and you may have no trouble with Excel. But on some people's machines (including mine), this code will always cause an exception:
ExcelWorksheet1.Connect;Bear in mind that the server components are just uniform wrappers around COM interfaces - they can't ensure that the interfaces work as you'd hope. On my machine,
CoCreateInstance(CLASS_Worksheet, nil, CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IUnknown, Result);doesn't return a _Worksheet interface. So using the application component is the safest approach.
Once your application has started, you may want to use some other components to access its objects: the Worksheet and Workbook components of Excel, or the DoCmd of Access, or the MailItem of Outlook. All you have to do is connect the components to the interfaces they should apply to, like this:
ExcelWorkbook1.ConnectTo(ExcelApplication1.ActiveWorkbook); ExcelWorksheet1.ConnectTo(ExcelApplication1.ActiveSheet as _Worksheet); DoCmd1.ConnectTo(Access.DoCmd); MailItem1.ConnectTo(Outlook.CreateItem(olMailItem) as MailItem);Note that you may have to cast the interface to be the required kind, if a property returns only an IDispatch interface. And the interface must exist before you can connect to it - the ConnectTo method will not create a new workbook, for example.
If you're used to coding with variables, you may be tempted to write
ExcelWorkbook1 := ExcelApplication1.ActiveWorkbook;Resist this urge! It won't work.
When you've finished, you should Disconnect all your server components:
ExcelWorksheet1.Disconnect; ExcelWorkbook1.Disconnect; ExcelApplication1.Disconnect;Leaving one connected will cause you problems if the user later closes the application you're connected to.
The server components have events which you can access from the object inspector as with other Delphi components. They generally allow you to access the object concerned in the event by means of an OleVariant type method parameter. Using them is much the same as using any Delphi component's events, so I shan't add anything here - except to point out that these events are trappable even without using the components. For details of this, and helpful code and examples see Binh Ly's site.
The D5+ server components make starting up an application easier, and handling application events a breeze. The method overloading they do allows you to miss out some LCID and EmptyParam parameters. So why would anyone not want to use them?
Well, speed is the obvious reason. These components are wrappers around interfaces. Every call you would otherwise send straight to an interface now goes through the component first. And they don't do anything you couldn't do without them, once you've popped in some standard startup code (and Binh Ly's connection points code if you want to use events).
You can perhaps get the best of both worlds by using the application component to start up the app, but using variables instead for all the other objects. But whether that works for you will depend on the events you need to trap.
Here you can download the source files of projects using the server components. To run these projects, you'll obviously need to have the relevant program(s) installed on your computer.
Excel97 Word97 Outlook (tested with Outlook 98)