Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

I've been trying to creating MS Access 2010 addin in VS 2010 Express in C# .NET 2.0. I implemented the IDTExtensibility2 interface, and created a function for addin registration, all code is shown below.

AddIn:
C#
using Microsoft.Office.Core;
using System;
using System.Runtime.InteropServices;
using ACCESS = Microsoft.Office.Interop.Access;

namespace SimpleAddIn
{
    [GuidAttribute("9A275102-2CD4-4AAF-9D57-F706C94F76D8"), ProgId("SimpleAddIn.Connect"), ComVisible(true)]
    public class Connect : Extensibility.IDTExtensibility2
    {
        private ACCESS.Application oApplication = null;
        private COMAddIn oAddIn = null;

        public void OnAddInsUpdate(ref Array custom)
        {

        }

        public void OnBeginShutdown(ref Array custom)
        {
            System.Windows.Forms.MessageBox.Show("Shutting down");
        }

        public void OnConnection(object Application, Extensibility.ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
        {
            Application = oApplication as ACCESS.Application;
            AddInInst = oAddIn as COMAddIn;

            if (ConnectMode == Extensibility.ext_ConnectMode.ext_cm_AfterStartup)
            {
                OnStartupComplete(ref custom);
            }
        }

        public void OnDisconnection(Extensibility.ext_DisconnectMode RemoveMode, ref Array custom)
        {
            if (RemoveMode == Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
            {
                OnBeginShutdown(ref custom);
            }
            oApplication = null;
        }

        public void OnStartupComplete(ref Array custom)
        {
            System.Windows.Forms.MessageBox.Show("Start up complete");
        }
    }
}


Register/Unregister functions:
C#
[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type type)
{
    try
    {
        Assembly thisAssembly = Assembly.GetAssembly(typeof(SimpleAddIn.Connect));
        RegistryKey key = Registry.ClassesRoot.CreateSubKey("CLSID\\{" + type.GUID.ToString().ToUpper() + "}\\InprocServer32\\1.0.0.0");
        key.SetValue("CodeBase", thisAssembly.CodeBase);
        key.Close();

        key = Registry.ClassesRoot.CreateSubKey("CLSID\\{" +  type.GUID.ToString().ToUpper() + "}\\InprocServer32");
        key.SetValue("CodeBase", thisAssembly.CodeBase);
        key.Close();

        
        // http://support.microsoft.com/kb/948461
       key = Registry.ClassesRoot.CreateSubKey("Interface\\{000C0601-0000-0000-C000-000000000046}");
       string defaultValue = key.GetValue("") as string;
       string s = defaultValue;
       key.SetValue("", "Office .NET Framework Lockback Bypass Key");
       key.Close();

       Registry.ClassesRoot.CreateSubKey(@"CLSID\{" + type.GUID.ToString().ToUpper() + @"}\Programmable");
       Registry.CurrentUser.CreateSubKey(_addinOfficeRegistryKey + "SimpleAddIn.Connect");
       RegistryKey rk = Registry.CurrentUser.OpenSubKey(_addinOfficeRegistryKey + "SimpleAddIn.Connect", true);
       rk.SetValue("LoadBehavior", Convert.ToInt32(3));
       rk.SetValue("FriendlyName", "Simple Acces COM AddIn");
       rk.SetValue("Description", "COM AddIn for MS Access 2010 written in C#");
       rk.Close();
   }
   catch (Exception ex)
   {
   }
}

[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type type)
{
    try
    {
        Registry.ClassesRoot.DeleteSubKey(@"CLSID\{" + type.GUID.ToString().ToUpper() + @"}\Programmable", false);
        Registry.CurrentUser.DeleteSubKey(_addinOfficeRegistryKey + "SimpleAddIn.Connect", false);
    }
    catch (Exception ex)
    {
    }
}


Results:
I tried this on two computers and got 2 different results:

1. Windows 7 x63, Office Pro x64 Visual Studio Ultimate installed.
- On this machine addin has been registered succesfully and is functioning

2. Windows 7x63, Office Pro x32, Visual Studio 2012 Express.
- On this machine I have managed to partially register the addin. I can see it in COM AddIns under MS Acces, but it is unloaded and I haven't managed to load it. Also on this machine I couldn't find registry key "{000C0601-0000-0000-C000-000000000046}" in CLSID/Interface/ so I manually created it and gave him a value "Office .NET Framework Lockback Bypass Key" and still addin wasn't loaded.

Any answers or clues would be much apprecieted, thanks.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900