Microsoft Office XP 和 Microsoft Office 2003 都支持一種新的統一的設計結構,這種結構用于生成應用程序外接程序以增強和控制 Office 應用程序。這些外接程序叫做 COM 外接程序。本文逐步討論了 Office COM 外接程序,并介紹了如何使用 Microsoft Visual C# .NET 生成 Office COM 外接程序。?
IDTExensibility2 接口
COM 外接程序是一種進程內 COM 服務器或 ActiveX 動態鏈接庫 (DLL),它實現如 Microsoft 外接程序設計器類型庫 (Msaddndr.dll) 中所描述的?IDTExensibility2?接口。所有 COM 外接程序都從此接口繼承而來,而且都必須實現其五個方法中的每一個方法。?
OnConnection
每當連接 COM 外接程序時,都會激發?OnConnection?事件。外接程序可以在啟動時連接、由最終用戶連接或者通過自動化來連接。如果?OnConnection?成功地返回,就表明已加載了外接程序。如果返回錯誤消息,那么宿主應用程序就立即釋放其對該外接程序的引用,而且該對象將被銷毀。
OnConnection?使用下列四個參數:
Application?— 一個對宿主應用程序對象的引用。
ConnectMode?— 一個指定外接程序連接方式的常量。外接程序可以采取下列幾種方式連接:
ext_cm_AfterStartup?— 外接程序由最終用戶從?COM 外接程序?對話框啟動。
ext_cm_CommandLine?— 外接程序從命令行連接。注意,此方式不適用于生成 Office 應用程序的 COM 外接程序。
ext_cm_External?— 外接程序由外部應用程序通過自動化連接。請注意,此方式不適用于生成 Office 應用程序的 COM 外接程序。
在?Connect?類中實現?IDTExtensibility2?的成員的代碼,如下所示: public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) {applicationObject = application;addInInstance = addInInst;if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup){OnStartupComplete(ref custom);}}public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom) {if(disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown){OnBeginShutdown(ref custom);}applicationObject = null;
}public void OnAddInsUpdate(ref System.Array custom)
{
}public void OnStartupComplete(ref System.Array custom)
{CommandBars oCommandBars;CommandBar oStandardBar;try{oCommandBars = (CommandBars)applicationObject.GetType().InvokeMember("CommandBars", BindingFlags.GetProperty , null, applicationObject ,null);}catch(Exception){// Outlook has the CommandBars collection on the Explorer object.object oActiveExplorer;oActiveExplorer= applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);oCommandBars= (CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,oActiveExplorer,null);}// Set up a custom button on the "Standard" commandbar.try{oStandardBar = oCommandBars["Standard"]; }catch(Exception){// Access names its main toolbar Database.oStandardBar = oCommandBars["Database"]; }// In case the button was not deleted, use the exiting one.try{MyButton = (CommandBarButton)oStandardBar.Controls["My Custom Button"];}catch(Exception){object omissing = System.Reflection.Missing.Value ;MyButton = (CommandBarButton) oStandardBar.Controls.Add(1, omissing , omissing , omissing , omissing);MyButton.Caption = "My Custom Button";MyButton.Style = MsoButtonStyle.msoButtonCaption;}// The following items are optional, but recommended. //The Tag property lets you quickly find the control //and helps MSO keep track of it when more than//one application window is visible. The property is required//by some Office applications and should be provided.MyButton.Tag = "My Custom Button";// The OnAction property is optional but recommended. //It should be set to the ProgID of the add-in, so that if//the add-in is not loaded when a user presses the button,//MSO loads the add-in automatically and then raises//the Click event for the add-in to handle. MyButton.OnAction = "!<MyCOMAddin.Connect>";MyButton.Visible = true;MyButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);object oName = applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);// Display a simple message to show which application you started in.System.Windows.Forms.MessageBox.Show("This Addin is loaded by " + oName.ToString() , "MyCOMAddin");oStandardBar = null;oCommandBars = null;
}public void OnBeginShutdown(ref System.Array custom)
{object omissing = System.Reflection.Missing.Value ;System.Windows.Forms.MessageBox.Show("MyCOMAddin Add-in is unloading.");MyButton.Delete(omissing);MyButton = null;
}private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool cancel) {System.Windows.Forms.MessageBox.Show("MyButton was Clicked","MyCOMAddin"); }
生成并測試該 COM 外接程序。為此,請按照下列步驟操作:
在生成菜單上,單擊生成解決方案。請注意,生成 COM 外接程序的過程中實際上就向 COM interop 注冊了 .NET 類。
啟動一個您選作外接程序的宿主應用程序的 Office 應用程序(例如,Microsoft Word 或 Microsoft Excel)。