Add-in with C#

To create a VBE add-in for Excel using C# you can use Visual Studio 2022.
Create a [[COM Add-in]] that implements the IDTExtensibility2 interface.
Project Name - VBACOMAddin
Add two additional references.
(1) Assemblies, Extensions: Microsoft.Vbe.Interop
(2) References, Add Reference, COM, Add "Microsoft Visual Basic for Applications Extensibility 5.3" (VBIDE)
Replace the 2 GUID Ids (www.guidgen.com)
Add the following code changes to the MyConnect.cs file

public class MyConnect : IMyConnect 
{
   private Microsoft.Vbe.Interop.VBE _VBEObject;


Creating


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 Entry

Create the following registry entry

HKEY_CURRENT_USER\Software\Microsoft\Office\Excel\Addins\VBACOMAddin.MyConnect 
(String)(Default) - (value not set)
(String) Description - VBA COM Addin
(String) FriendlyName - VBACOMAddin
(DWord) LoadBehavior - 3

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.



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext