Call MS Access macro from c# code

Posted on 2007-07-23
Last Modified: 2010-05-18
Is it possible to call an Access macro using c# code?  Can you use ADO to do this?  What would the code look like?  
Question by:adwiseman
    LVL 8

    Accepted Solution

    LVL 77

    Expert Comment

    Just emphasising that this requires a copy of Access on the machine where you will run the code.
    LVL 14

    Author Comment

    That KB article works says macros but it's actually running an Access module, ie VBScript.  It doesn't seem to work for Access macros.  The error I get whenever I try to call a macro with this code is:

    An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

    BTW, I'm using Access 2003 and .net 1.1.  Is there any reason I need to be at .net 2.0?
    LVL 14

    Author Comment

    I found the solution.  Here's the code I used:

                            Access.ApplicationClass oAccess = new Access.ApplicationClass();
                      oAccess.Visible = true;
                      oAccess.OpenCurrentDatabase("c:\\db1.mdb", false, "");

                      // Run the macro
                      oAccess.DoCmd.RunMacro(("myMacro" as Object), (1 as Object),
                            (true as Object));

                      Console.WriteLine("Macro Complete");

                      // Quit Access and clean up.
                      System.Runtime.InteropServices.Marshal.ReleaseComObject (oAccess);
                      oAccess = null;

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
    A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now