//uses Excel2000
TExcelExpand = class(TAutoObject, _IDTExtensibility2)
private
FExcelApp : TExcelApplication;
FExcelSheet : TExcelWorksheet;
procedure OnWorkbookActivate(ASender: TObject; const Wb: ExcelWorkbook);
procedure OnChange(ASender: TObject; const Target: ExcelRange);
protected
procedure OnAddInsUpdate(var custom: PSafeArray); safecall;
procedure OnBeginShutdown(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
destructor Destroy; override;
end;
procedure TExcelExpand.OnConnection(const Application: IDispatch;
ConnectMode: ext_ConnectMode; const AddInInst: IDispatch;
var custom: PSafeArray);
begin
if not Assigned(FExcelApp) then
FExcelApp := TExcelApplication.Create(nil);
FExcelApp.ConnectTo(FApp);
FExcelApp.OnSheetActivate := OnSheetActive;
end;
procedure TExcelExpand.OnWorkbookActivate(ASender: TObject;
const Wb: ExcelWorkbook);
begin
if not Assigned(FExcelSheet) then
FExcelSheet := TExcelWorkSheet.Create(nil);
FExcelSheet.ConnectTo(FExcelApp.ActiveSheet as _WorkSheet);
FExcelSheet.OnChange := OnChange;//}
end;
procedure TExcelExpand.OnChange(ASender: TObject;
const Target: ExcelRange);
var
s : string;
begin
s := Target.Cells.Item[1,1];
messagebox(0, PChar(s), '', 0);
end;
这是用Office Addin技术实现的,注册这个Addin后,打开Excel,切换一下Sheet(新建的workbook不会触发OnWorkbookActivate),然后修改单元格内容,就会得到通知了