COM Add-in

Introduced in Excel 2000.
COM Add-ins must be in-process COM servers that support the Extensibility.IDTExtensibility2 interface.
If you want an add-in to appear in the COM Add-ins dialogs then it must implement this interface.
Creating a COM Add-in for the Visual Basic Editor - msdn2.microsoft.com/en-us/library/aa165206(office.10).aspx


Creating

Open Visual Studio 2017.
New Project, Class Library (.NET Framework) Change the Name to "VBACOMAddin"
Change the Location to somewhere on your C drive.
Add three references to this project (Project > Add Reference) References, Add Reference, Assemblies, Framework, Add "System.Windows.Forms"
References, Add Reference, Assemblies, Extensions, Add "Extensibility"
References, Add Reference, COM, Add "Microsoft Visual Basic for Applications Extensibility 5.3" (Version 5.3)
Rename the Class1.cs file to MyConnect.cs.
Add the following interface to the MyConnect.cs file.


namespace VBACOMAddin1 
{
    [System.Runtime.InteropServices.ProgId("VBACOMAddin.MyConnect")]
    [System.Runtime.InteropServices.Guid("C7F6486B-BAFC-420D-AE5E-3143BFC36E3E")]
    [System.Runtime.InteropServices.ComVisible(true)]
    public class MyConnect
    {
        public Microsoft.Vbe.Interop.VBE _ApplicationObject;
        public Microsoft.Office.Core.COMAddIn _AddinInstance;

        public void OnConnection(
            object Application,
            Extensibility.ext_ConnectMode ConnectMode,
            object AddInInst,
            ref System.Array custom)
        {
            this._ApplicationObject = (Microsoft.Vbe.Interop.VBE)Application;

            if (this._AddinInstance == null)
            {
                this._AddinInstance = (Microsoft.Office.Core.COMAddIn)AddInInst;
                this._AddinInstance.Object = this;
            }

            switch (ConnectMode)
            {
                case Extensibility.ext_ConnectMode.ext_cm_Startup:
                    // normal execution path - don't initialize just yet, wait for OnStartupComplete to be called by the host.
                    break;
                case Extensibility.ext_ConnectMode.ext_cm_AfterStartup:
                    InitializeAddIn();
                    break;
            }
        }
        public void OnDisconnection(
            Extensibility.ext_DisconnectMode RemoveMode,
            ref System.Array custom)
        {
            this._ApplicationObject = null;
            this._AddinInstance = null;
        }
        public void OnAddInsUpdate(ref System.Array custom)
        { }
        public void OnStartupComplete(ref System.Array custom)
        {
            InitializeAddIn();
        }
        public void OnBeginShutdown(ref System.Array custom)
        { }

        public void InitializeAddIn()
        {
            System.Windows.Forms.MessageBox.Show(
                this._AddinInstance.ProgId + " loaded in VBA editor version " +
                this._ApplicationObject.Version);
        }
    }
}

Make sure the Guid Id is replaced using (Tools > Create GUID).
Select Build > Build Solution).
At this point no registry entries are added to the registry.
Select (Project > Properties).
Display the Build tab.
Tick the "Register for COM Interop" checkbox (requires admin rights).
Close the Properties window.
Select (Build > Build Solution).
When the solution is built a "TLB" file appears in the "\bin\Debug\" folder.
When the solution is built the RegAsm tool automatically runs.
The RegAsm tool adds the necessary class and interface information to the registry.

C:\Windows\Microsoft.NET\Framework\v4.0.30319\ RegAsm /codebase VBACOMAddin.dll 

Registry Keys - COM Add-in

Microsoft Office COM add-ins are identified by reading from the registry.
You can view the registry by going to the Windows start menu, choosing Run, typing regedit and pressing OK.
There must be a key representing the COM Add-in under the Addins key:
64 Bit Office - HKEY_CURRENT_USER\Software\Microsoft\VBA\VBE\7.1\Addins64\
32 Bit Office - HKEY_CURRENT_USER\Software\Microsoft\VBA\VBE\7.1\Addins\


HKEY_CURRENT_USER\Software\Microsoft\VBA\VBE\7.1\Addins\VBACOMAddin.MyConnect 
(String)(Default) - (value not set)
(String) Description - A Long Description
(String) FriendlyName - VBACOMAddin
(DWord) LoadBehavior - 0

Description - A brief description of the add-in.
FriendlyName - A user friendly name that is displayed in the COM add-ins dialog box.
Load Behavior - The load behaviour of the add-in.



© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrev