Excel Macro from C#
-
Hi all, I am calling excel macro function which is available in excel template file. I get the reference of the work sheet, and iam trying to calling a macro from C#.The code is as below. System.Diagnostics.Process[] myProcesses; Excel.ApplicationClass appInstanceReference; myProcesses = System.Diagnostics.Process.GetProcessesByName("EXCEL"); if ( myProcesses.Length > 0 ) { Excel.Workbook xlwkbook = (Excel.Workbook) System.Runtime.InteropServices.Marshal.BindToMoniker(System.Windows.Forms.Application.StartupPath + @"\Templates\CommercialSizing.XLT"); if ( xlwkbook != null ) { appInstanceReference = (Excel.ApplicationClass) xlwkbook.Application; if ( appInstanceReference != null) { appInstanceReference.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, appInstanceReference,new object[] {"'" + System.Windows.Forms.Application.StartupPath + @"\Templates\CommercialSizing.XLT" + "'!YourMacro"} ); } } } The problem is, it is not just calling macro function, instead of that, it is opening new excel template and it is calling macro function, but the excel sheet is already opened, i just need to execute my macro.what is the change i need to do in my code.Thanks for your help.