参照WORD按钮添加的方法,现在在EXCEL中工具栏是加上了,但是按钮加不上,不知道怎么修改以下代码:
library exceladdin;
uses
ComServ,
exceladdin_TLB in 'exceladdin_TLB.pas',
AddInDesignerObjects_TLB in 'E:/Program Files/Borland/Delphi7/Projects/AddInDesignerObjects_TLB.pas',
main in 'main.pas' {addintest: CoClass},
cmdbarbtn in 'cmdbarbtn.pas';
exports
DllGetClassObject,
DllCanUnloadNow,
DllRegisterServer,
DllUnregisterServer;
{$R *.TLB}
{$R *.RES}
begin
end.
===================
unit main;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
ComObj, ActiveX, exceladdin_TLB, StdVcl, AddInDesignerObjects_TLB,
excel2000,office2000,cmdbarbtn;
type
Taddintest = class(TAutoObject, _IDTExtensibility2)
private
FexcelApp : TexcelApplication;
FCommandBarButton : TCommandBarButton;
procedure FClick(const Ctrl: OleVariant;
var CancelDefault: OleVariant);
protected
procedure OnAddInsUpdate(var custom: PSafeArray);
safecall;
procedure Onbegin
Shutdown(var custom: PSafeArray);
safecall;
procedure OnConnection(const Application: IDispatch;
ConnectMode: ext_ConnectMode;
const AddInInst: IDispatch;
var custom: PSafeArray);
safecall;
procedure OnDisconnection(RemoveMode: ext_DisconnectMode;
var custom: PSafeArray);
safecall;
procedure OnStartupComplete(var custom: PSafeArray);
safecall;
public
property excelApp : TexcelApplication read FexcelApp;
end;
implementation
uses ComServ,dialogs;
procedure Taddintest.OnAddInsUpdate(var custom: PSafeArray);
begin
end;
procedure Taddintest.Onbegin
Shutdown(var custom: PSafeArray);
begin
end;
procedure Taddintest.OnConnection(const Application: IDispatch;
ConnectMode: ext_ConnectMode;
const AddInInst: IDispatch;
var custom: PSafeArray);
var
WA : excel2000._Application;
acommandbar:commandbar;
aButton:_CommandBarButton;
emptyparam:OleVariant;
begin
//ShowMessage('Hello excel, Delphi is here!');
FexcelApp := TexcelApplication.Create(nil);
WA := Application as excel2000._Application;
excelApp.ConnectTo(WA);
ShowMessage('Connected to ' + excelApp.Name);
aCommandBar:=excelapp.CommandBars.Add('delphitest',msoBarTop,false,true);
aButton := aCommandBar.Controls.Add(msoControlButton, emptyparam, emptyparam,emptyparam, true) as _CommandBarButton;
aButton.Set_Style(msoButtonIconAndCaption);
aButton.Set_Caption('myTest');
aButton.Set_Tag('test111');
FCommandBarButton := TCommandBarButton.Create(nil);
FCommandBarButton.ConnectTo(aButton);
FCommandBarButton.OnClick := FClick;
aCommandBar.Set_Visible(True);
end;
procedure Taddintest.OnDisconnection(RemoveMode: ext_DisconnectMode;
var custom: PSafeArray);
begin
FCommandBarButton.Disconnect;
FCommandBarButton.Free;
end;
procedure Taddintest.OnStartupComplete(var custom: PSafeArray);
begin
end;
procedure Taddintest.FClick(const Ctrl: OleVariant;
var CancelDefault: OleVariant);
begin
//Ctrl.Application.Selection.TypeText(DateTimeToStr(Now) + #13#10);
showmessage('click me');
end;
initialization
TAutoObjectFactory.Create(ComServer, Taddintest, Class_addintest,
ciMultiInstance, tmApartment);
end.
===========================
unit cmdbarbtn;
interface
uses oleserver,office2000,ActiveX,Classes;
type
TCommandBarButtonClick = procedure(const Ctrl: OleVariant;
var CancelDefault: OleVariant) of Object;
TCommandBarButton = class(TOleServer)
private
FIntf: CommandBarButton;
FOnClick: TCommandBarButtonClick;
function GetDefaultInterface: CommandBarButton;
procedure SetOnClick(const Value: TCommandBarButtonClick);
protected
procedure InitServerData;
override;
procedure InvokeEvent(DispID: TDispID;
var Params: TVariantArray);
override;
public
constructor Create(AOwner: TComponent);
override;
destructor Destroy;
override;
procedure Connect;
override;
procedure ConnectTo(svrIntf: CommandBarButton);
procedure Disconnect;
override;
property DefaultInterface: CommandBarButton read GetDefaultInterface;
published
property OnClick : TCommandBarButtonClick read FOnClick write SetOnClick;
end;
implementation
{ TCommandBarButton }
procedure TCommandBarButton.Connect;
var
punk: IUnknown;
begin
if FIntf = nil then
begin
punk := GetServer;
ConnectEvents(punk);
Fintf:= punk as CommandBarButton;
end;
end;
procedure TCommandBarButton.ConnectTo(svrIntf: CommandBarButton);
begin
Disconnect;
FIntf := svrIntf;
ConnectEvents(FIntf);
end;
constructor TCommandBarButton.Create(AOwner: TComponent);
begin
inherited;
end;
destructor TCommandBarButton.Destroy;
begin
inherited;
end;
procedure TCommandBarButton.Disconnect;
begin
if Fintf <> nil then
begin
DisconnectEvents(FIntf);
FIntf := nil;
end;
end;
function TCommandBarButton.GetDefaultInterface: CommandBarButton;
begin
if FIntf = nil then
Connect;
Assert(FIntf <> nil, 'DefaultInterface is NULL. Component is not connected to Server. You must call ''Connect'' or ''ConnectTo'' before this operation');
Result := FIntf;
end;
procedure TCommandBarButton.InitServerData;
const
CServerData: TServerData = (
ClassID: '{55F88891-7708-11D1-ACEB-006008961DA5}';
IntfIID: '{000C0351-0000-0000-C000-000000000046}';
EventIID: '{000C0351-0000-0000-C000-000000000046}';
LicenseKey: nil;
Version: 500);
begin
ServerData := @CServerData;
end;
procedure TCommandBarButton.InvokeEvent(DispID: TDispID;
var Params: TVariantArray);
begin
case DispID of
-1: Exit;
// DISPID_UNKNOWN
1: if Assigned(FOnClick) then
FOnClick(Params[0], Params[1]);
end;
{case DispID}
end;
procedure TCommandBarButton.SetOnClick(
const Value: TCommandBarButtonClick);
begin
FOnClick := Value;
end;
end.