|
|
|||
Home Products Downloads Registered users Support Prices Order Primary Subscription |
|
||
More useful exampleWhere? \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 }
end;
procedure AddIn_Initialize; override; procedure AddIn_Finalize; override; procedure AddIn_ControlEvent(const Tag: string; ICtrl: IaxpControl); override; 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,
end.
Class_MySecondAddIn, [ohaExcel], 'MySecondAddIn',
'This is my second COM add-in for MS Excel'); 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;
begin
IsNew: boolean; ICBox: IaxpComboBox;
inherited;
end;
DM := TDataModule1.Create(nil); try
DM.Table1.Active := true;
finally
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,
end;
EmptyParam);
DM.Table1.Next;
DM.Free;
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
end;
HostApp.Selection.Value := (ICtrl as IaxpComboBox).Text;
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 informationUnfortunately, 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. |
Developed forDelphi 5, 6, 7
[ Download it ] Immediate shipment
|