我做了个automation object来自己捕获excel的workbookopen和workbookbeforeclose事件,但是为什么没有反应呢?代码如下
unit Project1_TLB;{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers.
{$WARN SYMBOL_PLATFORM OFF}
{$WRITEABLECONST ON}
{$VARPROPSETTER ON}
interfaceuses Windows, ActiveX, Classes, Graphics, StdVCL, Variants;
const
Project1MajorVersion = 1;
Project1MinorVersion = 0; LIBID_Project1: TGUID = '{000F7D8E-614F-48F3-9C32-E8F018D15915}'; IID_IEventTest: TGUID = '{B2B2E24F-BE87-4EF8-91BB-97E9B8FFD31A}';
CLASS_EventTest: TGUID = '{139309F5-81A9-490E-9DAC-11909EDB477E}';
type
IEventTest = interface;
IEventTestDisp = dispinterface; EventTest = IEventTest; IEventTest = interface(IDispatch)
['{B2B2E24F-BE87-4EF8-91BB-97E9B8FFD31A}']
procedure WorkbookOpen(const Wb: IDispatch); safecall;
end; IEventTestDisp = dispinterface
['{B2B2E24F-BE87-4EF8-91BB-97E9B8FFD31A}']
procedure WorkbookOpen(const Wb: IDispatch); dispid 1567;
end; CoEventTest = class
class function Create: IEventTest;
class function CreateRemote(const MachineName: string): IEventTest;
end;implementationuses ComObj;class function CoEventTest.Create: IEventTest;
begin
Result := CreateComObject(CLASS_EventTest) as IEventTest;
end;class function CoEventTest.CreateRemote(const MachineName: string): IEventTest;
begin
Result := CreateRemoteComObject(MachineName, CLASS_EventTest) as IEventTest;
end;end.
-----------------------------------------unit Unit2;{$WARN SYMBOL_PLATFORM OFF}interfaceuses
ComObj, ActiveX, Project1_TLB, StdVcl, Dialogs;const
evtguid : TGUID = '{00024413-0001-0000-C000-000000000046}';type
TEventTest = class(TAutoObject, IEventTest)
private
fconn : Integer;
protected
procedure WorkbookBeforeClose(var Wb: OleVariant; var Cancel: WordBool);
safecall;
procedure WorkbookOpen(const Wb: IDispatch); safecall;
public
function ObjQueryInterface(const IID:TGUID; out Obj):HResult;
override;stdcall;
constructor Create(app:OleVariant); overload;
Destructor Destroy; override;
end;implementationuses ComServ;constructor TEventTest.Create(app: OleVariant);
begin
fconn := 0;
InterfaceConnect(app, evtguid,Self,fconn);
end;destructor TEventTest.Destroy;
begin
InterfaceDisconnect(self, evtguid, fconn);
inherited;
end;function TEventTest.ObjQueryInterface(const IID: TGUID; out Obj): HResult;
begin
if IsEqualGUID(IID, evtguid) and GetInterface(IDispatch,Obj) then
Result := S_OK
else
Result := inherited ObjQueryInterface(IID, Obj);
end;procedure TEventTest.WorkbookBeforeClose(var Wb: OleVariant;
var Cancel: WordBool);
begin
//showmessage('close');
//cancel := True;
end;procedure TEventTest.WorkbookOpen(const Wb: IDispatch);
begin
showmessage('open');
end;initialization
TAutoObjectFactory.Create(ComServer, TEventTest, Class_EventTest,
ciInternal, tmApartment);
end.
unit Project1_TLB;{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers.
{$WARN SYMBOL_PLATFORM OFF}
{$WRITEABLECONST ON}
{$VARPROPSETTER ON}
interfaceuses Windows, ActiveX, Classes, Graphics, StdVCL, Variants;
const
Project1MajorVersion = 1;
Project1MinorVersion = 0; LIBID_Project1: TGUID = '{000F7D8E-614F-48F3-9C32-E8F018D15915}'; IID_IEventTest: TGUID = '{B2B2E24F-BE87-4EF8-91BB-97E9B8FFD31A}';
CLASS_EventTest: TGUID = '{139309F5-81A9-490E-9DAC-11909EDB477E}';
type
IEventTest = interface;
IEventTestDisp = dispinterface; EventTest = IEventTest; IEventTest = interface(IDispatch)
['{B2B2E24F-BE87-4EF8-91BB-97E9B8FFD31A}']
procedure WorkbookOpen(const Wb: IDispatch); safecall;
end; IEventTestDisp = dispinterface
['{B2B2E24F-BE87-4EF8-91BB-97E9B8FFD31A}']
procedure WorkbookOpen(const Wb: IDispatch); dispid 1567;
end; CoEventTest = class
class function Create: IEventTest;
class function CreateRemote(const MachineName: string): IEventTest;
end;implementationuses ComObj;class function CoEventTest.Create: IEventTest;
begin
Result := CreateComObject(CLASS_EventTest) as IEventTest;
end;class function CoEventTest.CreateRemote(const MachineName: string): IEventTest;
begin
Result := CreateRemoteComObject(MachineName, CLASS_EventTest) as IEventTest;
end;end.
-----------------------------------------unit Unit2;{$WARN SYMBOL_PLATFORM OFF}interfaceuses
ComObj, ActiveX, Project1_TLB, StdVcl, Dialogs;const
evtguid : TGUID = '{00024413-0001-0000-C000-000000000046}';type
TEventTest = class(TAutoObject, IEventTest)
private
fconn : Integer;
protected
procedure WorkbookBeforeClose(var Wb: OleVariant; var Cancel: WordBool);
safecall;
procedure WorkbookOpen(const Wb: IDispatch); safecall;
public
function ObjQueryInterface(const IID:TGUID; out Obj):HResult;
override;stdcall;
constructor Create(app:OleVariant); overload;
Destructor Destroy; override;
end;implementationuses ComServ;constructor TEventTest.Create(app: OleVariant);
begin
fconn := 0;
InterfaceConnect(app, evtguid,Self,fconn);
end;destructor TEventTest.Destroy;
begin
InterfaceDisconnect(self, evtguid, fconn);
inherited;
end;function TEventTest.ObjQueryInterface(const IID: TGUID; out Obj): HResult;
begin
if IsEqualGUID(IID, evtguid) and GetInterface(IDispatch,Obj) then
Result := S_OK
else
Result := inherited ObjQueryInterface(IID, Obj);
end;procedure TEventTest.WorkbookBeforeClose(var Wb: OleVariant;
var Cancel: WordBool);
begin
//showmessage('close');
//cancel := True;
end;procedure TEventTest.WorkbookOpen(const Wb: IDispatch);
begin
showmessage('open');
end;initialization
TAutoObjectFactory.Create(ComServer, TEventTest, Class_EventTest,
ciInternal, tmApartment);
end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货