Calling an Excel Macro from X++
All, recently I needed to know how to call an Excel Macro from X++. This is not something that is widely published anywhere on the web, and something I had to figure out.
It's actually not that hard, and the best help was looking at how I had done it before in .Net.
So here is the code example:
SysExcelApplication ExcelApp;
SysExcelWorkbooks WorkBooks;
SysExcelWorkbook WorkBook;
SysExcelWorksheet Worksheet;
;
ExcelApp = SysExcelApplication::construct();
ExcelApp.displayAlerts(true);
ExcelApp.visible(true);
ExcelApp.workbooks().open("SomeExcelFile.xls");
Workbook = ExcelApp.workbooks().item(1);
Worksheet = Workbook.worksheets().itemFromName("WorkSheetName");
ExcelApp.comObject().Run("SomeMacro"); // The Call to run a Macro
Of course there is a lot more you can do, but basically any Excel based class has a .comObject() at which you can further extend and make Excel Interop COM calls via, that go beyond what is coded for in the SysExcel Class framework.
Check back soon!
"Visit the Dynamics AX Community Page today!"
It's actually not that hard, and the best help was looking at how I had done it before in .Net.
So here is the code example:
SysExcelApplication ExcelApp;
SysExcelWorkbooks WorkBooks;
SysExcelWorkbook WorkBook;
SysExcelWorksheet Worksheet;
;
ExcelApp = SysExcelApplication::construct();
ExcelApp.displayAlerts(true);
ExcelApp.visible(true);
ExcelApp.workbooks().open("SomeExcelFile.xls");
Workbook = ExcelApp.workbooks().item(1);
Worksheet = Workbook.worksheets().itemFromName("WorkSheetName");
ExcelApp.comObject().Run("SomeMacro"); // The Call to run a Macro
Of course there is a lot more you can do, but basically any Excel based class has a .comObject() at which you can further extend and make Excel Interop COM calls via, that go beyond what is coded for in the SysExcel Class framework.
Check back soon!
"Visit the Dynamics AX Community Page today!"
Labels: Code Example, Dynamics AX, Excel, Excel 2003, Excel 2007, X++, Xpp
5 Comments:
Useful post!
Is it possible to open a workbook whilst selecting the enable macros option from AX code?
Would it be possible to transfer a worksheet to another workbook in AX?
Hi,
I want to insert new row in excel using macro in x++. I used tried this code but i got error. I don't know how to do this, please help me.. thanks.
This is the code:
ExcelApp.comObject().Run("ActiveCell.EntireRow.Insert");
please help me on how to achieve this.. more power..
Regards,
AML
Hi,
Is this correct?
ExcelApp.comObject().Run("ActiveCell.EntireRow.Insert");
Regards,
AML
Try to use this instead:
Com excelRange, entireRow;
;
excelRange = xlsWorkSheet.cells().range(strfmt("%1%3:%2%3","A","A",18)).comObject();
entireRow = excelRange.entireRow();
entireRow.insert();
Post a Comment
<< Home