Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7872789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:18:56+00:00 2026-06-03T02:18:56+00:00

I am injecting a piece of VBA code into a Microsoft Access database from

  • 0

I am injecting a piece of VBA code into a Microsoft Access database from .Net.

It is just a single line of code, which runs a Macro. All the Macro does is run a block of VBA code inside a module.

The issue I am having is that this all happens in a new MSAccess session, which I can’t even see, instead of the session the user currently has open.

Is it possible to, instead, have this interact with the users current MSAccess session? The whole point of this is to open a particular form inside the MSAccess session on every occurence of a .Net event. My C# code is below:

using Microsoft.Office.Interop.Access;

var msAccess = new Application();

msAccess.OpenCurrentDatabase(@"x:\foo\bar.accdb", false);
msAccess.DoCmd.RunMacro("macCTI");
msAccess.CloseCurrentDatabase();

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:18:57+00:00Added an answer on June 3, 2026 at 2:18 am

    If you have a single instance of Access running you could use Marshal.GetActiveObject:

    using Microsoft.Office.Interop.Access;
    using System.Runtime.InteropServices
    ...
    try
    {
        var msAccess = (Application)Marshal.GetActiveObject("Access.Application");
        msAccess.DoCmd.RunMacro("macCTI");
    }
    catch (COMException ex)
    {
        // handle error
    }
    

    Alternatively, if more than one are running, and no two instances have the same database open, you could use Marshal.BindToMoniker:

    var msAccess = (Application) Marshal.BindToMoniker(@"x:\foo\bar.accdb"); 
    

    Note: This Microsoft knowledge base article has this to say:

    Whether a COM server is Single Use (Multiple Instances) or
    Multiuse (Single Instance) might affect your decision to use
    GetActiveObject to get reference to that server. Because potentially
    more than one instance of Word, Excel, or Microsoft Access can be
    running, GetActiveObject on a particular server may return an instance
    that you did not expect. The instance that is first registered in the
    ROT is typically the instance that is returned by GetActiveObject. If
    you want to get an Automation Reference to a specific running instance
    of Word, Excel, or Microsoft Access, use BindToMoniker with the name
    of the file that is opened in that instance. For a Multiuse (Single
    Instance) server like PowerPoint, it does not matter, because the
    automation reference points to the same running instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a piece of code which inserts a link into a p tag
Is there a way to prevent Castle Windsor from automatically injecting dependencies into properties
I would like to write a piece of code for inserting a number into
I've got a piece of code that calls the DeleteFile method in the Microsoft.VisualBasic.FileIO.FileSystem
I'm injecting JavaScript code into a website for personal use, using a Google Chrome
I have a piece of legacy VB6 code that is inserting data into a
I have a piece of code that works in a background process which looks
I'm injecting a dependency CheckCompatibRepository . I'm mocking a method IsCompatible which has a
I am injecting javascript into a page via autohotkey by entering javascript:blahblahblah into the
I'm customising the Group Headers on a Telerik RadGrid by injecting a LinkButton into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.