Microsoft Excel reporting and 
 data analyzing with practically no coding. 
 .Net, ActiveX, and VCL versions. 
 www.AfalinaSoft.com  

Home    Products    Downloads    Registered users    Support    Prices    Order    Primary Subscription

 

MS Office COM Add-ins. Microsoft Excel reporting 
 and data analyzing


More useful example - Office Add-in Express Developer's Guide

<< Previous

Table of contents

Next >>


More useful example

Where? \QDemo\Second\SecondAddin.dpr

Let's assume that your users work with Excel filling in some form and one of the fields in the form should contain a value from a DB. Let's create an add-in that places a combo box containing country names onto a separate toolbar. We expect that choosing a value in the combo box will put the value to the selected cells on the active worksheet.

To create such COM Add-in we have executed the above-described step#1 and have registered the ActiveX server (Run|Register ActiveX Server). And as a result we have the following code:

unit SecondAddIn_IMPL;

interface

uses
ComObj, ComServ, ActiveX, axpAddIn, SecondAddIn_TLB;

type
TMySecondAddIn = class(TaxpAddIn, IMySecondAddIn)
protected
{ Protected declarations }
procedure AddIn_Initialize; override;
procedure AddIn_Finalize; override;
procedure AddIn_ControlEvent(const Tag: string; ICtrl: IaxpControl); override;
end;

implementation

uses Data;

const
BarName = 'Countries';
cbCountryTag = 'COUNTRIES';

{ TMySecondAddIn }

procedure TMySecondAddIn.AddIn_ControlEvent(const Tag: string; ICtrl: IaxpControl);
begin
end;

procedure TMySecondAddIn.AddIn_Initialize;
begin
inherited;
end;

procedure TMySecondAddIn.AddIn_Finalize;
begin
inherited;
end;

initialization
TaxpFactory.CreateEx(ComServer, TMySecondAddIn,
Class_MySecondAddIn, [ohaExcel], 'MySecondAddIn',
'This is my second COM add-in for MS Excel');
end.

Now we have to consider how we initialize the add-in and how we process its events. Whenever the add-in runs, we should open the Country table from DBDEMOS and populate the combo box with values from this table. Then we should better close the table because we will not need it. Right?

Let's add a DataModule and place one TTable in it. Link the TTable component with the Country table from DBDEMOS. Give the DataModule quite an unpretentious name - Data. Let's refer to the DataModule in the uses clause and implement AddIn_Initialize:

implementation

uses ComServ, Data;

const
BarName = 'Countries';
cbCountryTag = 'COUNTRIESTAG';

{ TMySecondAddIn }

procedure TMySecondAddIn.AddIn_Initialize;
var
DM: TDataModule1;
IsNew: boolean;
ICBox: IaxpComboBox;
begin
inherited;
DM := TDataModule1.Create(nil);
try
DM.Table1.Active := true;
AddCmdBar(BarName, IsNew);
ICBox := AddComboBox(BarName, 'Country:', cbCountryTag,
'Select country', msoComboLabel, true, IsNew);
ICBox.Clear;
while not DM.Table1.EOF do begin
ICBox.AddItem(DM.Table1.FieldByName('Name').AsString,
EmptyParam);
DM.Table1.Next;
end;
finally
DM.Free;
end;
end;

Here we call the inherited method, create the DataModule, and open the table. Then we create a new CommandBar with the name specified by the BarName constant. Pay attention to the next line (ICBox := AddComboBox...). The Style parameter of the AddComboBox method allows adding a label to the combo box. Passing true to the DropDown parameter is similar to using the csDropDownList combobox style. (False corresponds to csDropDown) All the methods of the TaxpAddIn class that add controls to CommandBar are functions returning references to interfaces of appropriate controls. So, AddComboBox returns the reference to the IaxpComboBox (CommandBarComboBox) interface thus allowing us performing "delicate manipulations" with the combo box: we Clear the list and add countries via AddItem.

Well, that's all with initialization. And now to the final (and simpler) task - we need to process the user's choice. The developers familiar with Excel can do it easily. But even they will need to know that TaxpAddIn contains a property referring to the interface of the host application - TaxpAddIn.HostApp (OLEVariant):

procedure TMySecondAddIn.AddIn_ControlEvent(const Tag: string;
ICtrl: IaxpControl);
begin
if Tag = cbCountryTag then
HostApp.Selection.Value := (ICtrl as IaxpComboBox).Text;
end;

If you paste this code to your project correctly, you will see that when Excel starts the new toolbar appears that contains the combo box needed. Have we held the task down? <g>




Some more information

Unfortunately, we can't describe in more or less detailed way how to work with all the interfaces of MS Office applications. The reason is simple - there are too many of them (you can count yourself: Excel, Word, Outlook, PowerPoint, Access, Project, FrontPage), while we have limited work force. So we recommend you to use the VBA online help of Office applications. And don't forget about early binding and imported type libraries that are used by Add-in Express.

<< Previous

Table of contents

Next >>



Developed for

Delphi 5, 6, 7
Excluding Delphi Personal


[ Download it ]

[ Order now ]

Immediate shipment




ADX Toys™

Free sample
add-ins with a complete source code based on Add-in Express.

[  Read more  ]




We are Borland 
 technology partner


Copyright © 1999-2006
All right reserved.
Privacy Policy

Write to WebMaster

Page Top
Add-in Express - COM Add-ins, Smart Tags and RTDS in C#, VB, C++, J#, and Delphi