1

I'm writing a COM add-in for the VBE of access and I want to execute a vba-function out of C# after clicking a commandbar button.

So I use the following code:

const string ApplicationObjectName = "Access.Application";
Microsoft.Office.Interop.Access.Application app = (Microsoft.Office.Interop.Access.Application)Marshal.GetActiveObject(ApplicationObjectName);
app.Run(functionName);

This works fine if there is only one ms-access-db open. But if there are two open databases, ´GetActiveObject´ gets the wrong application and the function is called in the other database. Not in the one the commandbar button is part of.

So, how do I get the correct application object (= the one the button is clicked in)?

Gener4tor
  • 414
  • 3
  • 12
  • 40

1 Answers1

1

At the moment I use the snippet from here (german only):

https://dotnet-snippets.de/snippet/laufende-com-objekte-abfragen/526

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

namespace Rainbird.Tools.COMInterop
{
    public class RunningObjectTable
    {
        private RunningObjectTable() { }

        [DllImport("ole32.dll")]
        private static extern int GetRunningObjectTable(uint reserved, out IRunningObjectTable pprot);

        [DllImport("ole32.dll")]
        private static extern int CreateBindCtx(uint reserved, out IBindCtx pctx);

        public static object GetRunningCOMObjectByName(string objectDisplayName)
        {
            IRunningObjectTable runningObjectTable = null;

            IEnumMoniker monikerList = null;

            try
            {
                if (GetRunningObjectTable(0, out runningObjectTable) != 0 || runningObjectTable == null) return null;

                runningObjectTable.EnumRunning(out monikerList);

                monikerList.Reset();

                IMoniker[] monikerContainer = new IMoniker[1];

                IntPtr pointerFetchedMonikers = IntPtr.Zero;

                while (monikerList.Next(1, monikerContainer, pointerFetchedMonikers) == 0)
                {
                    IBindCtx bindInfo;

                    string displayName;

                    CreateBindCtx(0, out bindInfo);

                    monikerContainer[0].GetDisplayName(bindInfo, null, out displayName);

                    Marshal.ReleaseComObject(bindInfo);

                    if (displayName.IndexOf(objectDisplayName) != -1)
                    {
                        object comInstance;
                        runningObjectTable.GetObject(monikerContainer[0], out comInstance);
                        return comInstance;
                    }
                }
            }
            catch
            {
                return null;
            }
            finally
            {
                if (runningObjectTable != null) Marshal.ReleaseComObject(runningObjectTable);
                if (monikerList != null) Marshal.ReleaseComObject(monikerList);
            }
            return null;
        }

        public static IList<string> GetRunningCOMObjectNames()
        {
            IList<string> result = new List<string>();

            IRunningObjectTable runningObjectTable = null;

            IEnumMoniker monikerList = null;

            try
            {
                if (GetRunningObjectTable(0, out runningObjectTable) != 0 || runningObjectTable == null) return null;

                runningObjectTable.EnumRunning(out monikerList);

                monikerList.Reset();

                IMoniker[] monikerContainer = new IMoniker[1];

                IntPtr pointerFetchedMonikers = IntPtr.Zero;


                while (monikerList.Next(1, monikerContainer, pointerFetchedMonikers) == 0)
                {
                    IBindCtx bindInfo;

                    string displayName;

                    CreateBindCtx(0, out bindInfo);

                    monikerContainer[0].GetDisplayName(bindInfo, null, out displayName);

                    Marshal.ReleaseComObject(bindInfo);

                    result.Add(displayName);
                }
                return result;
            }
            catch
            {
                return null;
            }
            finally
            {

                if (runningObjectTable != null) Marshal.ReleaseComObject(runningObjectTable);
                if (monikerList != null) Marshal.ReleaseComObject(monikerList);
            }
        }
    }
}

with this code (works only for ms-access):

var activeProject = m_VBE.ActiveVBProject;
Microsoft.Office.Interop.Access.Application app = (Microsoft.Office.Interop.Access.Application)RunningObjectTable.GetRunningCOMObjectByName(activeProject.FileName);
app.Run(functionName);

But there has to be a better solution to this problem.

Similar issues are also discussed here: How do I get the *actual* host application instance? and here: How to use Marshal.getActiveObject() to get 2 instance of of a running process that has two processes open

Gener4tor
  • 414
  • 3
  • 12
  • 40