• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6303
  • Last Modified:

Excel Interop - running a Macro from C#

Hi,

I am programmatically accessing an Excel Workbook via C# using the Excel.Interop assembly.

The workbook contains a custom Macro.

Is there a way of programmatically running this Macro?

I've tried:
 Workbook.Run ("MacroName")
But, that requires 30 additional parameters?!

Many Thanks.




0
Ibs
Asked:
Ibs
1 Solution
 
Expert1701Commented:
You can either fill all those parameters with System.Reflection.Missing.Value.

  excel.Run("YourMacroName", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, ..., System.Reflection.Missing.Value);

or you can use reflection to invoke the Run method,

  excel.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, excel, new object[] { "YourMacroName" });

For more information, see:

  HOW TO: Run Office Macros by Using Automation from Visual C# .NET
  http://support.microsoft.com/kb/306683/en-us
0
 
IbsAuthor Commented:
Outstanding! Thank you very much.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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