先贴上部分已经实现的WORD插件代码。数字签名部分正在封装。希望大家帮助!!!
unit AddInMain;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
ComObj, ActiveX, Dialogs, SignaturePro_TLB, StdVcl, AddinDesignerObjects_TLB, Word_TLB, Office_TLB, Variants
,BtnSvr//BtnSvr是对Office公开的CommandBarButton对象的Delphi封装,自己手工创建该封装
,Word2000, Office2000//要引用该两个单元
,Signature;
type
TAddIn = class(TAutoObject, IAddIn, IDTExtensibility2)
private
FWordApp : TWordApplication; //TWordApplication是在Word2000中封装
FSignatureObj: TSignature;
DICommandBar : CommandBar; //CommandBar是OFFICE2000中封装
WriteBtn : TButtonServer; //TButtonServer是BtnSvr中封装
DIBtn2 : TButtonServer; //TButtonServer是BtnSvr中封装
DIBtn3 : TButtonServer; //TButtonServer是BtnSvr中封装
DIMenu : TButtonServer; //TButtonServer是BtnSvr中封装
protected
{ Protected declarations }
//IDTExtensibility2 methods
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 OnAddInsUpdate(var custom: PSafeArray); safecall;
procedure OnStartupComplete(var custom: PSafeArray); safecall;
procedure OnBeginShutdown(var custom: PSafeArray); safecall;
public
property WordApp : TWordApplication read FWordApp;
end;
implementation
uses ComServ;
{ TAddIn }
procedure TAddIn.OnAddInsUpdate(var custom: PSafeArray);
begin
end;
procedure TAddIn.OnBeginShutdown(var custom: PSafeArray);
begin
end;
procedure TAddIn.OnConnection(const Application: IDispatch;
ConnectMode: ext_ConnectMode; const AddInInst: IDispatch;
var custom: PSafeArray);
var
WA : word2000._Application;
begin
FWordApp := TWordApplication.Create(nil);
FSignatureObj := TSignature.Create();
WA := Application as word2000._Application;
WordApp.ConnectTo(WA);//创建WordApp连接到WORD,后面要释放
//ShowMessage('FGB数字签名插件连接到' + WordApp.Name);
end;
procedure TAddIn.OnDisconnection(RemoveMode: ext_DisconnectMode;
var custom: PSafeArray);
begin
//ShowMessage('FGB数字签名插件从Word释放');
WriteBtn.Free;
DIBtn2.Free;
DIBtn3.Free;
FSignatureObj.Free;
FWordApp.Free;
WordApp.Free;//释放对象
end;
procedure TAddIn.OnStartupComplete(var custom: PSafeArray);
var
MenuIntf, BtnIntf, BtnIntf2, BtnIntf3 : CommandBarControl;
i : integer;
ToolsBar : CommandBar;
begin
DICommandBar := nil;
for i := 1 to WordApp.CommandBars.Count do
begin
if (WordApp.CommandBars.Item.Name = '数子签名插件') then
DICommandBar := WordApp.CommandBars.Item;
end; // see if we alreadt registered the command bar with Word
if (Not Assigned(DICommandBar)) then begin
DICommandBar := WordApp.CommandBars.Add('数子签名插件', EmptyParam, EmptyParam, EmptyParam);
DICommandBar.Set_Protection(msoBarNoCustomize);
end;
//ShowMessage(VarToStr(DICommandBar.Controls.Count));
if (DICommandBar.Controls.Count > 0) then
begin
BtnIntf := DICommandBar.Controls.Item[1];
BtnIntf2 := DICommandBar.Controls.Item[2];
BtnIntf2 := DICommandBar.Controls.Item[3];
end
else
begin
BtnIntf := DICommandBar.Controls.Add(msoControlButton,
EmptyParam, EmptyParam, EmptyParam, EmptyParam);
BtnIntf2 := DICommandBar.Controls.Add(msoControlButton,
EmptyParam, EmptyParam, EmptyParam, EmptyParam);
BtnIntf3 := DICommandBar.Controls.Add(msoControlButton,
EmptyParam, EmptyParam, EmptyParam, EmptyParam);
end;
WriteBtn := TButtonServer.Create(nil);
WriteBtn.ConnectTo(BtnIntf as _CommandBarButton);
WriteBtn.Caption := '手写签名';
WriteBtn.Style := msoButtonCaption;
WriteBtn.Visible := true;
WriteBtn.OnClick := FSignatureObj.WriteBtnClick;
DIBtn2 := TButtonServer.Create(nil);
DIBtn2.ConnectTo(BtnIntf2 as _CommandBarButton);
DIBtn2.Caption := '电子印章';
DIBtn2.Style := msoButtonCaption;
DIBtn2.Visible := true;
DIBtn3 := TButtonServer.Create(nil);
DIBtn3.ConnectTo(BtnIntf3 as _CommandBarButton);
DIBtn3.Caption := '盖章设定';
DIBtn3.Style := msoButtonCaption;
DIBtn3.Visible := true;
DICommandBar.Set_Visible(true);
end;
initialization
TAutoObjectFactory.Create(ComServer, TAddIn, Class_AddIn,
ciMultiInstance, tmApartment);
end.