Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Excel macros from VS?

Tags:

c#

excel

How to run Excel macros from VS2010(C#)?I use Microsoft.Office.Interop.Excel namespace

like image 361
Ilya Blokh Avatar asked Nov 01 '10 18:11

Ilya Blokh


People also ask

How do I run a macro in Excel without the Developer tab?

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.

How do I use macros in Visual Basic Excel?

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.)

How do I automatically run a macro in Excel?

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.


2 Answers

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);
}
like image 144
amarsuperstar Avatar answered Nov 10 '22 19:11

amarsuperstar


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.

like image 28
Falcon Avatar answered Nov 10 '22 19:11

Falcon