[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3583
  • Last Modified:

Call MS Access macro from c# code

Is it possible to call an Access macro using c# code?  Can you use ADO to do this?  What would the code look like?  
0
adwiseman
Asked:
adwiseman
  • 2
1 Solution
 
sabeeshCommented:
0
 
peter57rCommented:
Just emphasising that this requires a copy of Access on the machine where you will run the code.
0
 
adwisemanAuthor Commented:
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?
0
 
adwisemanAuthor Commented:
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.
                  oAccess.DoCmd.Quit(Access.AcQuitOption.acQuitSaveNone);
                  System.Runtime.InteropServices.Marshal.ReleaseComObject (oAccess);
                  oAccess = null;
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now