How to: Generate Office COM Outline using Visual Basic .NET
Article ID: 302896 Last Update Date: February 13, 2004 Version: 1.0
The release number of this article has been CHS302896
For Microsoft Visual C # .NET versions of this article, see
302901.
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
• Application • Ondisconnection • OnaddinsUpdate • OnStartupComplete and OnBeginshutdown • COM Add / COM Add / COM Uploads • How to generate a COM add-in using Visual Basic. NET • Separation example • Reference
This page
Summary reference
summary
Microsoft Office 2000 and later support a new unified design structure that generates 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 access and describes how to generate Office COM add-in using Microsoft Visual Basic .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 returns successfully, 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 external program connection. The add-on can be connected in the following ways:
• 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 Gene COM add-in. • 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 the host application. • Custom - An 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 will be excited before it is uninstalled from memory
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: • EXT_DM_HOSTSHUTDOWN - The add-on is disconnected when the host application is turned off. • EXT_DM_USERCLOSED - The add-in is disconnected by the end user or automation controller. • Custom - An 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 / SOFTWARE / Microsoft / Office / OfficeApp / Addins / PROGID
The add-in can provide a value of the display name and a complete description of this location. In addition, the add-in should use a DWORD value called Loadbehavior to specify the desired loading 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 starts. • 8 = DemandLoad - loads only when requested by the user. • 16 = ConnectFirstTime - only one time (when started next time). Typically 0x03 (Connected | BootLoad) is typically specified.
Achieved
The address of IDTextensibility2 should also specify a DWORD value called CommandLinesafe to point out that the add-in is safe for operations that do not support user interfaces. The value is 0x00 to represent false, and the value is 0x01 indicates true.
Back to top
Using Visual Basic .NET Generates a COM Output As described above, the Office COM add-in is a COM server in the process of running at COM running at the time of COM. Therefore, in order to develop COM external connections in Visual Basic .NET, the add-in 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 Basic .NET, follow these steps:
1. In Visual Basic .NET, create a class library project. 2. Add a reference to the type library that implements IDTEXTENSIBILITY2. The main interface set of this item has appeared under the EXTENSIBILITY name. 3. Add a reference to the Microsoft Office object library. The main interface set of this item has appeared under the Office name. 4. Create a public class in the class library that implements the IDTEXTENSibility2. 5. After generating this class, the library is registered to 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. 6. Create a registry entry to enable the Office application to identify and load an add-in. You can choose to complete all these steps, you can also create NET items for shared add-in. This will launch the Extended Wizard, which helps you create a COM add-in in .NET.
The Extended Wizard will create a Visual Basic .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
1. On the File menu of Microsoft Visual Studio .NET, click New, and then click Project. 2. In the New Project dialog box, expand other items under the project type, select the extension item, and then select the shared add-in template. 3. Type MyComAddin as the name of the add-in, and then click OK. 4. After the Scalability Wizard appears, follow these steps:
a. On page 1, choose to create an add-in using Visual Basic, and then click Next. b. On page 2, select the Host Application below, then click Next:
• Microsoft Word • Microsoft PowerPoint • Microsoft Outlook • Microsoft Excel • Microsoft Accessc. 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. d. In page 4, select all available options, and then click Next. e. Click Finish. 5. Add the following members to the Connect class: Dim WithEvents MyButton As CommandBarButton 6. IDTExtensibility2 code that implements all members in the Connect class, as follows: Public Sub OnBeginShutdown (ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2. Onbeginshutdown
ON Error ResMe next
'Notify the user you are shutting down, and delete the button.
MsgBox ("Our Custom Add-in Unlineing.") MyButton.delete ()
MyButton = Nothing
End Sub
Public Sub onaddinsUpdate (byref custom as system.Array) Implements extensibility.idtextensibility2.onaddinsupdate
'
End Sub
Public Sub onstartupComplete (Byref Custom As System.Array) Implements Extensibility.idtextens Ility2.onstartupComplete
Dim Ocommandbars as Commandbars
Dim Ostandardbar as Commandbar
ON Error ResMe next
'Set Up A Custom Button on The "Standard" Command Bar.
Ocommandbars = ApplicationObject.commandbars
IF OCommandbars is nothing then
'Outlook Has The Commandbars Collection on The Explorer Object.
Ocommandbars = ApplicationObject.ActiveExplorer.commandbars
END IF
Ostandardbar = Ocommandbars.Item ("standard")
IF ostandar is nothing then
'Access Names ITS Main Toolbar Database.
Ostandardbar = Ocommandbars.Item ("Database")
END IF
'In Case The Button Was Not Deleted, Use the exitation one.
MyButton = OstandardBar.Controls.Item ("My Custom Button")
IF mybutton is nothing then
MyButton = ostandardbar.controls.add (1)
WITH MYBUTTON
.Caption = "My Custom Button"
.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 at des
'One Application Window Is Visible. The Property Is Required
'by Some Office Applications and Should Be Provided.
.Tag = "My Custom Button"
'The onaction property is optional but recommended.
'It Should Be set to the progid of the add-in, so whatiff
'the add-in not loaded WHEN A USER CLICKS The Button,' Mso Loads The Add-in Automatic and Then Raises
'The click evenet for the add-in to handle.
.OnAction = "!
.Visible = TRUE
End with
END IF
'Display a Simple Message to Show Which Application You Started in.
"" Started in "& ApplicationObject.name &")
Ostandardbar = Nothing
Ocommandbars = Nothing
End Sub
Public Sub OnDisconnection (Byval RemoveMode as Extensibility.ext_disconnectMode, Byref Custom As System.Array) Implements Extensibility.idtextensibility2.ondisConnection
ON Error ResMe next
IF removemode <> extensibility.ext_disconnectMode.ext_dm_hostshutdown then_
Call onbeginshutdown (Custom)
ApplicationObject = Nothing
End Sub
Public Sub OnConnection (ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
MsgBox ("on connections in myaddin)
ApplicationObject = Application
AddinInstance = AddinInst
'If you am't in Startup, Manually Call onstartupComplete.
IF (ConnectMode <> EXTENSIBILITY.EXT_CONNECTMODE.EXT_CM_STARTUP) THEN _
Call onstartupComplete (Custom)
End Sub
Private sub mybutton_click (byval ctrl as microsoft.office.core.commandbutton, byref canceldefault as boolean) Handles mybutton.click
Msgbox ("Our Commandbar Button Was Pressed!")
End Sub
7. Generate and test the COM external connector. To do this, follow these steps:
a. On the Generative Menu, click Generate MyComaddin. Note that the .NET class is registered to COM Interop during the process of generating a COM external connector. b. Start a Office application for host applications you selected as an external program (for example, Microsoft Word or Microsoft Excel). c. After the external program starts, the onConnection event will be fired and you will receive a message box. After closing the message box, you will be excited, and you will receive the second message box. Please turn off the message box. d. Please note that the add-on adds a new direct button for "My Custom Button" to the standard toolbar. e. 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. f. Exit the Office application. g. When you exit the application, you will inspire the OnBeginsHutDown event and you receive a message box. Close this message box to end the presentation. Back to top
reference
For additional information about writing a COM external program, click the article number below to see the article in the Microsoft Knowledge Base:
190253 Info: VB6 Designers Do Not Work in VB5
For more information on developing web-based solutions for Microsoft Internet Explorer, please visit the following Microsoft Web site:
http://msdn.microsoft.com/workshop/entry.asp
http://msdn.microsoft.com/ie/
http://support.microsoft.com/highlights/iep.asp?fr=0&sd=msdn
(c) Microsoft Corporation 2001, all rights reserved. Provided by Microsoft Corporation's Ranjit R. Sawant.
Back to top
The information in this article applies to:
• Microsoft Visual .NET 2002 Standard Edition • Microsoft Office Excel 2003 • Microsoft Excel 2002 Standard Edition • Microsoft Office Outlook 2003 • Microsoft Outlook 2002 Standard Edition • Microsoft Office PowerPoint 2003 • Microsoft PowerPoint 2002 Standard Edition • Microsoft Office Word 2003 • Microsoft Word 2002 Standard version
Back to top
Keywords: kbhowtomaster kbautomation kb302896
Back to top
(source:
http://support.microsoft.com/kb/302896)