Generate Office COM add-in using Visual C # .NET
Suitable
For Microsoft Visual Basic .NET versions of this article, see
. For the Microsoft Visual Basic 6.0 version of this article, see
Move 238228. For Microsoft Visual C 6.0, please refer to this article
230689.
This task content
Summary
IDTEXENSIBILITY2 interface
OnConnection OnSconnection OnaddinsUpdate OnStartupComplete and Onbeginshutdown COM Add Upworking How to Use Visual C # .NET Generate COM Add-Size Step Sample Reference
Overview Microsoft Office XP and Microsoft Office 2003 support a new unified design structure, which is used to generate an application add-in to enhance and control Office applications. These external programs are called COM external programs. This article gradually discusses the Office COM external program and introduces how to generate Office COM add-in using Microsoft Visual C # .NET.
Back to top
The IDTexensibility2 interface COM add-in-one COM server or ActiveX Dynamic Link Library (DLL), which implements as described in the Microsoft External Designer Type Library (MsadDr.dll)
IDTEXENSIBILITY2 interface. All COM add-in is inherited from this interface, and every way in its five methods must be implemented.
Back to top
OnConnection, whenever the COM add-in
ONCONNECTION event. The add-on can be connected to the end user or by automation at startup. in case
OnConnection successfully returned, an add-in has been loaded. If an error message is returned, the host application immediately releases its reference to the external program, and the object will be destroyed.
ONCONNECTION uses the following four parameters:
Application - A reference to the host application object. ConnectMode - A constant for specifying an add-in connection mode. The add-on can be connected in the following ways:
The EXT_CM_AFTERSTARTUP - the add-in is started by the end user from the COM Add Directions dialog. EXT_CM_COMMANDLINE - The add-on is connected to the command line. Note that this method does not apply to the COM add-in to generate the Office application. EXT_CM_EXTERNAL - The add-in is connected by an external application. Note that this method does not apply to the COM external connector to generate the Office application. The EXT_CM_STARTUP - the add-in is started by the host when the application starts. This behavior is controlled by the settings in the registry. AddInST - A reference to ComadDin objects that reference this additional procedure in the ComadDins collection of host applications. Custom - A array containing a Variant type value that stores user-defined data.
Back to top
OnDisconnection will be excited when the COM external program is disconnected and
OnDisconnection event. The add-in should perform all resource cleanup operations in this event and restore any changes to the host application.
OnDisConnection uses the following two parameters:
RemoveMode - A constant that specifies the way an external program is open. The add-on can be disconnected in the following manner:
The EXT_DM_HOSTSHUTDOWN-The add-on is disconnected when the host application is turned off. The ext_dm_userclosed - the add-in is disconnected by the end user or automation controller. Custom - A array containing a Variant type value that stores user-defined data. Back to top
ONADDINSUPDATE will stimulate when the registered COM external assembly changes
ONADDINSUPDATE event. In other words, this event is excited whenever a COM add-in or when it is removed from the host application.
Back to top
OnStartUpComplete and Onbeginshutdown When the host application is busy loading itself or uninstalling itself from memory, avoiding user interaction, and
OnStartupComplete and
The OnBeginshutDown method is called when the host application has left or is being to enter this state. Only calls when the add-in is connected during the startup
OnStartUpComplete, only the host is disconnected in the closing process to disconnect the connection to the add-in.
Onbeginshutdown.
Since the user interface of the host application is completely active while inspiring these events, they may be the only way to perform certain operations, and they will not be available from other ways.
ONCONNECTION and
These operations are performed in OnDisConnection.
Back to top
The COM Add-Access Upon registration, the COM add-in need to register itself every Office application running. In order to register itself to a particular application, the add-in should use its progID as an item name to create a child in the following position:
HKEY_CURRENT_USER / OFFTWARE / Microsoft / Office / OfficeApp / AddINS / Progid AddPay can provide values for a promising display name and complete instructions. In addition, the external program should use a named
The DWORD value of Loadbehavior specifies the desired load behavior. This value determines how the host application loads an add-in, and it consists of the combination of the following value:
0 = disconnect - unloaded. 1 = Connected - has been loaded. 2 = bootload - Loads when the application is started. 8 = DemandLoad - is loaded only when requested by the user. 16 = ConnectFirstTime - only load once (when started next time). Typically 0x03 (Connected | BootLoad) is typically specified.
Achieved
IDTEXTENSIBILITY2 should also specify a named
CommandLinesAfe's DWORD value to point out whether the add-in is safe for the operation that does not support the user interface. The value is 0x00 to represent false, and the value is 0x01 indicates true.
Back to top
How to use Visual C # .NET Generate COM Add-in, the Office COM add-in is a COM server in the process of running at the COM runtime layer by the Office application. Therefore, in order to develop COM external connections in .NET, the external program components need to be implemented in .NET, then disclose to the COM client (ie Office application) through the COM Interop layer.
To create a COM Add 2 in Visual C # .NET, follow these steps:
In Visual C # .NET, create a class library project. Add a reference to the type library that implements IDTextensibility2. The main interface set of this item has appeared under the EXTENSIBILITY name. Add a reference to the Microsoft Office object library. The main interface set of this item has appeared under the Office name. Create a public class in the class library that implements the IDTEXTENSibility2. After generating this class library, the library is registered with the COM Interop. To do this, you need to generate an assembly that uses a strong name for this class library and then registers it to COM Intero. You can use REGASM.EXE to register .NET components to COM Interop. Creating a registry entry to make Office applications to identify and load an add-in. You can choose to complete all these steps, or you can create a .NET project that is shared .NET project. This will launch the Extended Wizard, which helps you create a COM add-in in .NET.
"Scalability Wizard" will create a Visual C # .NET class library project, and create an implementation.
IDTEXTENSIBILITY2 interface
CNECT class. It also generates implementation
The main code of the empty member of IDTextensibility. This project has references to ExtensIlity and Office assemblies. The project has been selected in the generation settings.
Register COM Interop. The program set key (.snk) file will be generated and in AssemblyInfo.vb files
ASSEMBLYKEYFILE attributes are referenced.
In addition to class library projects, the wizard will also generate a installation project that can be used to deploy COM external connections on other computers. This item can be removed when needed.
Back to top
Step-by-step example
On the File menu of Microsoft Visual Studio. NT, click New, and then click the project. In the New Project dialog box, expand additional items under the project type, select the extended item, and then select the shared add-in template. Type MyComAddin as the name of the add-in, then click OK. After the Scalable Wizard appears, follow these steps:
On page 1, choose to create an add-in using Visual C #, and then click Next. On page 2, select the Host Application below, and then click Next:
Microsoft Word Microsoft PowerPoint Microsoft Outlook Microsoft Excel Microsoft Access On page 3, enter the name and description of the add-in, then click Next. Note: The name and description of the addr appear in the COM Add-in dialog of Office applications. In page 4, select all available options, then click Next. Click Finish. On the project menu, click Add Reference. Click System.Windows.Forms.dll in the Component list, click Select, and then click OK. Add the following code to the namespace list in the Connect class: Using system.reflection; add the following members to the Connect class: Private CommandbarButton MyButton; Implement IDTextens Ility2 members in the Connect class, as follows: public void onConnection (Object Application, ExtensIlity.ext_connectMode ConnectMode, Object Addininst, Ref System.Array Custom) {ApplicationObject = Application;
AddinInstance = AddinInst;
IF (ConnectMode! = EXTENSIBILITY.EXT_CONNECTMODE.EXT_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 exitation one.
Try
{
MyButton = (Commandbarbutton) ostandardbar.controls ["My Custom Button];
}
Catch (Exception)
{
Object omissing = system.reflection.Missing.Value;
MyButton = (Commandbarbutton) OstandardBar.controls.Add (1, OMISSING, OMISING, OMISSING, OMISSING);
MyButton.caption = "My Custom Button";
MyButton.Style = msobuttonstyle.msobuttoncaption;
}
// The Following Items Are Optional, But Recommended.
// the tag printy 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 provides.
MyButton.tag = "My Custom Button";
// The onaction property is optional but recommented.
// IT SHOULD be set to the progid of the add-in, so what if
// the add-in 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 = "!
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 (), "mycom 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 vid mybutton_click (commandbarbutton cmdbutton, ref boult) {
System.windows.Forms.MessageBox.Show ("MyButton Was Clicked", "MyComaddin");
Generate and test the COM external program. To do this, follow these steps:
On the Generative menu, click Generate Solutions. Note that the .NET class is registered to COM Interop during the process of generating a COM external connector. Launch a Office application for the host application you selected as an external program (for example, Microsoft Word or Microsoft Excel). After the external program starts, you will be excited that its OnStartUpComplete event will you receive a message box. Please turn off the message box. Note that the add-on adds a new custom button that adds a new title "My Custom Button" to the Standard Toolbar. Click My Custom Button (My Custom button). The Click event of this button will be processed by an add-in and you receive a message box. Please turn off the message box. Exit the Office application. When you exit the application, you will inspire the OnBeginsShutDown event and you receive a message box. Close this message box to end the presentation.