我的不是AddIn的,要做Addin,相信你会。
//主程序
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleServer, Word2000, Office2000, StdCtrls, wordTbButton, ImgList,
TB2Item, Clipbrd, Excel2000;
type
TForm1 = class(TForm)
app1: TWordApplication;
Button1: TButton;
ImageList1: TImageList;
Wd: TWordDocument;
Button2: TButton;
app: TExcelApplication;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure app1DocumentChange(Sender: TObject);
private
{ Private declarations }
FButtonNewDoc: TCommandBarButton;
aButton : _CommandBarButton;
function CreateButton(ACommandBar: CommandBar): _CommandBarButton;
procedure FClick(const Ctrl: OleVariant;
var CancelDefault: OleVariant);
public
{ Public declarations }
procedure CopyBmpToClp(imList: TImageList;
index: integer);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
aCommandBar : CommandBar;
begin
app.Connect;
// aCommandBar := App.CommandBars.Add('办公自动化', msoBarTop, False, True);
FButtonNewDoc := TCommandBarButton.Create(nil);
aButton := CreateButton(app.CommandBars.Item['Standard']);
aButton.Set_Caption('发放');
aButton.Set_TooltipText('将会议记录发给你的同事');
aButton.Set_Tag(aButton.Caption);
CopyBmpToClp(imageList1,0);
aButton.PasteFace;
FButtonNewDoc.ConnectTo(aButton);
FButtonNewDoc.OnClick := FClick;
app.Visible[0]:=true;
// aCommandBar.Set_Visible(True);
// app.
//Wd.ConnectTo(app.Documents.AddOld(emptyParam,EmptyParam));
//wd.Name:='eOAS-会议记录';
end;
procedure TForm1.CopyBmpToClp(imList: TImageList;
index: integer);
var
bmp: Tbitmap;
begin
with TClipboard.Createdo
begin
bmp:=Tbitmap.Create;
try
bmp.Height:=imList.Height;
bmp.Width:=imlist.Width;
imlist.Draw(bmp.Canvas,0,0,Index);
assign(bmp);
finally
bmp.Free;
free;
end;
end;
end;
function TForm1.CreateButton(ACommandBar: CommandBar): _CommandBarButton;
begin
Result := ACommandBar.Controls.Add(msoControlButton, EmptyParam, EmptyParam, 1, True) as _CommandBarButton;
with Resultdo
begin
Set_Style(msoButtonIconAndCaption);
end;
end;
procedure TForm1.FClick(const Ctrl: OleVariant;
var CancelDefault: OleVariant);
begin
//App.Selection.TypeText(DateTimeToStr(Now) + #13#10);
showmessage('这是Excel 的自定义按钮 传过来的,对不对?');
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
try
FButtonNewDoc.free;
//app.CommandBars.Item['办公自动化'].Delete;
except
//
end;
end;
procedure TForm1.app1DocumentChange(Sender: TObject);
begin
// if app.Documents.Count>0 then
// app.CommandBars.Item['Standard'].Controls['发放'].Set_Visible(app.ActiveDocument.Name=wd.Name);
end;
end.
//引用的程序
unit WordTbButton;
interface
uses Classes,ComObj, ActiveX, Office2000, Word2000, oleServer;
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
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
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;
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: '{000C030E-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.