Saturday 8 December 2012

Run Excel Macro from C#.net

using Excel = Microsoft.Office.Interop.Excel;
using VBIDE = Microsoft.Vbe.Interop;
using Office = Microsoft.Office.Core;

object misValue = System.Reflection.Missing.Value;
Object oMissing = System.Reflection.Missing.Value;

public void AddMacro()
{
    savepath = @"C:\Temp_999.xls";
    Excel.Application oExcel;
    Excel.Workbook oBook;
    VBIDE.VBComponent oModule;
    Office.CommandBar oCommandBar;
    Office.CommandBarButton oCommandBarButton;              
    oExcel = new Excel.Application();
    if (oExcel != null)
    {
        oBook = oExcel.Workbooks.Add(oMissing);
        oModule = oBook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);
        sCode = "<add your Macro Code>";
        oModule.CodeModule.AddFromString(sCode.Replace("'", "\""));
        oCommandBar = oExcel.CommandBars.Add("VBAMacroCommandBar", oMissing, oMissing, oMissing);
        oCommandBar.Visible = false;
        oCommandBarButton = (Office.CommandBarButton)oCommandBar.Controls.Add(Office.MsoControlType.msoControlButton, oMissing, oMissing, oMissing, oMissing);
        oCommandBarButton.OnAction = "VBAMacro";
        oCommandBarButton.Caption = "Call VBAMacro";
        oCommandBarButton.FaceId = 2151;
        oExcel.DisplayAlerts = false;
        oBook.SaveAs(savepath.ToString(), Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);       
        RunMacro();
     }
     public void RunMacro()    
     {
         Excel.ApplicationClass oExcel1 = new Excel.ApplicationClass();
        oExcel1.Visible = false;
        Excel.Workbooks oBooks1 = oExcel1.Workbooks;
        Excel._Workbook oBook1 = null;
        oBook1 = oBooks1.Open(savepath, oMissing, oMissing,oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
        RunMacro1(oExcel1, new Object[] { "Combineallworkbooksintosingleworkbook" });           
        Microsoft.Office.Interop.Excel.Application oExcelApp;
        VBIDE.VBComponent module;
        oExcelApp = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
        oExcelApp.DisplayAlerts = false;
        module = oExcelApp.ActiveWorkbook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);
        module.CodeModule.AddFromString(sCode.Replace("'", "\""));
        string date = DateTime.Now.ToString().Replace("/", "-").Replace(":", ".");   
        spath1 = "Excel_Combined_" + date + ".xls";
        spath = txtSaveas.Text + "\\" + spath1;
        oExcelApp.ActiveWorkbook.SaveAs(spath.ToString(), Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);   
       oExcelApp = null;
     }
     private void RunMacro1(object oApp, object[] oRunArgs)
     {
                 oApp.GetType().InvokeMember("Run",System.Reflection.BindingFlags.Default |System.Reflection.BindingFlags.InvokeMethod,null, oApp, oRunArgs);
      }   
}

2 comments:

  1. Which option is better ? Arrange all parts of xl in c# and after then make command macro to execute ♡or call a assembled xl including userform and after execute the programme

    ReplyDelete