How to run Excel macros from VS2010(C#)?I use Microsoft.Office.Interop.Excel namespace
There's no need to use the Developer or View tab since there is a dedicated keyboard shortcut to open the Macro menu. You can use the Alt + F8 keyboard shortcut to open the Macro menu.
Open your workbook in Excel. Press Alt + F11 to open Visual Basic Editor (VBE). Right-click on your workbook name in the "Project-VBAProject" pane (at the top left corner of the editor window) and select Insert -> Module from the context menu. Copy the VBA code (from a web-page etc.)
Using Auto open method to run a macro automatically: Insert a New Module from Insert Menu. Copy the above code and Paste in the code window. Save the file as macro enabled workbook. Open the workbook to test it, it will Run a Macro Automatically.
Try this article:
http://support.microsoft.com/kb/306683
The relevant part to run a macro is this (where oApp
is the application instance in your code):
private void RunMacro(object oApp, object[] oRunArgs)
{
oApp.GetType().InvokeMember("Run",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.InvokeMethod,
null, oApp, oRunArgs);
}
In addition to amarsuperstar's answer,
I'd like to state that you program needs to be a trusted source in order to call these macros. And the Excel-File itself needs to be trusted as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With