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?  
LVL 14
adwisemanAsked:
Who is Participating?
 
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;
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.