Office2000(2003) 下 outlook,word 的 com addin 之 delphi实现!(整理摸索)[2]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 andyle 的 blog

  intfiid:   '{a30af5ac-d1fd-486a-8c7e-f71416eafc4b}';
  eventiid:  {24bde880-d3db-4777-a5d4-ad24e1e3fcbd}';
  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;

4, 继续完成tmyoutlookaddin类
在类定义里面增加项
private
  fcommandbarbutton : tcommandbarbutton;
  fcommandbarbutton2 : tcommandbarbutton;
  procedure fclick(const ctrl: olevariant; var canceldefault: olevariant);
  procedure f2click(const ctrl: olevariant; var canceldefault: olevariant);

在onconnection写下面代码
procedure tmyoutlookaddin.onconnection(const application: idispatch;
connectmode: ext_connectmode; const addininst: idispatch;
var custom: psafearray);
//读取一个bitmap并复制到粘贴板
  procedure copybitmaptoclipboard(strfile : string);
  var
    abitmap : tbitmap;
  begin
    with tclipboard.create do
    begin
      try
        abitmap := tbitmap.create;
        abitmap.loadfromfile(strfile);
        assign(abitmap);
      finally
       abitmap.free;
       free;
     end;
   end;
 end;

var
  app : outlookapplication;          //word addin实现差别,定义 :wordapp: wordapplication
  acommandbar : commandbar;
  abutton,abutton2 : _commandbarbutton;
begin

  app := outlookapplication(application);
  acommandbar := app.activeexplorer.commandbars.add('newbuttonbar', msobartop, false, true); //button栏
//word addin实现差别
// app := wordapplication(application);
// acommandbar := app.commandbars.add('syni', msobartop, false, true); //button栏

//第一个button
  abutton := acommandbar.controls.add(msocontrolbutton, emptyparam, emptyparam, emptyparam, true)        as _commandbarbutton;
  abutton.set_style(msobuttoniconandcaption);
  abutton.set_caption('scp manager');
  copybitmaptoclipboard(yourbmpfile); //这两句话是给按钮设定一个外部图标,
  abutton.pasteface; //你要增加一个bitmap资源bitmap大小为16*16
  abutton.set_tag('manger');
  fcommandbarbutton := tcommandbarbutton.create(nil);
  fcommandbarbutton.connectto(abutton);//button关联
  fcommandbarbutton.onclick := fclick;//响应事件的赋值

//第二个button
  abutton2 := acommandbar.controls.add(msocontrolbutton, emptyparam, emptyparam, emptyparam,   true) as _commandbarbutton;
  abutton2.set_style(msobuttoniconandcaption);
  abutton2.set_caption('scp history');
  copybitmaptoclipboard(yourbmpfile2); //这两句话是给按钮设定一个外部图标,
  abutton2.pasteface; //
  abutton2.set_tag('history'); // 必须与其他button不同
  fcommandbarbutton2 := tcommandbarbutton.create(nil);
  fcommandbarbutton2.connectto(abutton2);
  fcommandbarbutton2.onclick := f2click; //响应事件2

  acommandbar.set_visible(true);
end;

在ondisconnection写下面代码
procedure tmyoutlookaddin.ondisconnection(removemode: ext_disconnectmode;
var custom: psafearray);
begin
  fcommandbarbutton.disconnect;
  fcommandbarbutton.free;
  fcommandbarbutton2.disconnect;
  fcommandbarbutton2.free;
end;

//写click事件(弹出一个dialog)
procedure tmyoutlookaddin.fclick(const ctrl: olevariant;
var canceldefault: olevariant);
begin
  showmessage('激活manager按钮click事件');
end;
 
//写click事件(执行一个程序)
procedure tmyoutlookaddin.f2click(const ctrl: olevariant;
var canceldefault: olevariant);
begin
  shellexecute(nil,'open','yourexepragram',nil,nil,sw_show);
end;

5, 最后编译注册。得打开regedit,像前面提到的那样写注册表。可以把它导出来,以后用到别的机器上的时候,直接合并就可以了。

6,运行outlook2000或者2003,就可以看到效果。
效果

参考文献: com add-ins : building a com add-in for outlook 2000  - (msdn ,thomas rizzo)
    office2000下内部com插件的编程实现 (徐景周)
   用delphi制作office的com addin    (cwxiao888)

需要代码的朋友,给你跟我联系,leyi.163@163.com

本文关键:outlook,word ,com addin
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top