locked
COM add-in with Word RRS feed

  • Question

  • User838769528 posted

     Hi, I'm trying to test an add-in in microsoft word, I followed the instructions in http://support.microsoft.com/?kbid=302901but I don't why it doesn't work. here is my code:

    namespace MyCOMAddin
    {
    	using System;
    	using Extensibility;
    	using System.Runtime.InteropServices;
        using System.Reflection;
        using Microsoft.Office.Core;
        
    
    	#region Read me for Add-in installation and setup information.
    	// When run, the Add-in wizard prepared the registry for the Add-in.
    	// At a later time, if the Add-in becomes unavailable for reasons such as:
    	//   1) You moved this project to a computer other than which is was originally created on.
    	//   2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
    	//   3) Registry corruption.
    	// you will need to re-register the Add-in by building the MyCOMAddinSetup project, 
    	// right click the project in the Solution Explorer, then choose install.
    	#endregion
    	
    	/// <summary>
    	///   The object for implementing an Add-in.
    	/// </summary>
    	/// <seealso class='IDTExtensibility2' />
    	[GuidAttribute("C645126A-CAA9-4179-ACCB-F54E836A8BA4"), ProgId("MyCOMAddin.Connect")]
    	public class Connect : Object, Extensibility.IDTExtensibility2
    	{
            
            private CommandBarButton MyButton; 
    
    		/// <summary>
    		///		Implements the constructor for the Add-in object.
    		///		Place your initialization code within this method.
    		/// </summary>
    		public Connect()
    		{
    		}
    
    		/// <summary>
    		///      Implements the OnConnection method of the IDTExtensibility2 interface.
    		///      Receives notification that the Add-in is being loaded.
    		/// </summary>
    		/// <param term='application'>
    		///      Root object of the host application.
    		/// </param>
    		/// <param term='connectMode'>
    		///      Describes how the Add-in is being loaded.
    		/// </param>
    		/// <param term='addInInst'>
    		///      Object representing this Add-in.
    		/// </param>
    		/// <seealso class='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);
                }
    
            }
    
    
    		/// <summary>
    		///     Implements the OnDisconnection method of the IDTExtensibility2 interface.
    		///     Receives notification that the Add-in is being unloaded.
    		/// </summary>
    		/// <param term='disconnectMode'>
    		///      Describes how the Add-in is being unloaded.
    		/// </param>
    		/// <param term='custom'>
    		///      Array of parameters that are host application specific.
    		/// </param>
    		/// <seealso class='IDTExtensibility2' />
            public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
            {
                if (disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
                {
                    OnBeginShutdown(ref custom);
                }
                applicationObject = null;
            }
    
    
    		/// <summary>
    		///      Implements the OnAddInsUpdate method of the IDTExtensibility2 interface.
    		///      Receives notification that the collection of Add-ins has changed.
    		/// </summary>
    		/// <param term='custom'>
    		///      Array of parameters that are host application specific.
    		/// </param>
    		/// <seealso class='IDTExtensibility2' />
            public void OnAddInsUpdate(ref System.Array custom)
            {
            }
    
    
    		/// <summary>
    		///      Implements the OnStartupComplete method of the IDTExtensibility2 interface.
    		///      Receives notification that the host application has completed loading.
    		/// </summary>
    		/// <param term='custom'>
    		///      Array of parameters that are host application specific.
    		/// </param>
    		/// <seealso class='IDTExtensibility2' />
            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;
            }
    
    
    		/// <summary>
    		///      Implements the OnBeginShutdown method of the IDTExtensibility2 interface.
    		///      Receives notification that the host application is being unloaded.
    		/// </summary>
    		/// <param term='custom'>
    		///      Array of parameters that are host application specific.
    		/// </param>
    		/// <seealso class='IDTExtensibility2' />
            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");
            }
    					
    		
    		private object applicationObject;
    		private object addInInstance;
    	}
    }

      But when I build all the Solution and secondly I launch the office word nothing occurs. I don't now why, anybody can help me?

    I also added the DWORD to the regedit, and changed the security level of the macros to medium. Maybe I have to create a .exe file to install the COM addin but the tutorial is very clearly. 

    Saturday, December 20, 2008 6:13 AM

All replies

  • User-1136466523 posted

    Hi,

    From your descripton, it seems that the above code does not work after you follow the instructions in the KB article, right?

    I'm not sure if you can provide more error details. Without details, what i can suggest you is try to check every steps in the KB article again, don't pay attention to the requirements that mentioned in the KB article.

    Thanks.

    Thursday, December 25, 2008 10:49 PM