Exporting from Matlab to excel with macro.
8 次查看(过去 30 天)
显示 更早的评论
I have matrix which I have to add in excel file. Excel file looks like in picture. With StartLoft, than StartCurve, than matrix which needs to be added, than EndCurve, EndLoft, End.
In this excel I should have embedded macro file to just open excel after starting matlab code, and run macro, without adding it additionaly. Is this possible? Macro code for excel is attached.
采纳的回答
Guillaume
2018-7-10
编辑:Guillaume
2018-7-10
My advice would be to have a blank excel file that already has the macro code in it. Whenever you want to create a new file with that macro, just copy that template file and fill it with xlswrite. This is probably the easiest and most reliable way.
Otherwise, yes it is possible to insert the macro from matlab, with some restrictions. The first problem you'll face is that for many versions now, Microsoft, by default, forbids programmatic access to VB projects (where macro code resides). The only way to enable programmatic access is within excel itself, under the Macro Settings of the Trust Center (in options), you need to tick Trust access to the VBA project object model. This cannot be done programmatically
Note that enabling this setting will make you more vulnerable to viruses.
Once that is done, you need to decide where that macro should reside: in a worksheet, in the workbook, or as a VB module. The import method differs for each of these. I'm going to assume that it's in the workbook as it's the most common case. To add the content of that text file to the workbook macro:
excel = actxserver('Excel.Application');
workbook = excel.Workbooks.Open(full_path_to_your_file);
vbproject = workbook.VBProject; %Will error is trust access to the VBA project model is not enabled
workbookcomponent = vbproject.Components.Item('ThisWorkook');
workbookcomponent.CodeModule.AddFromFile(full_path_to_your_text_file);
workbook.SaveAs(full_path_as_xlsm_file, 52); %52 is xlOpenXMLWorkbookMacroEnabled
excel.Quit;
更多回答(1 个)
Sayyed Ahmad
2018-7-10
you have to use activeX in matlab to start Excel
e = actxserver('excel.application');
e.visible=1;
%%new Woorkbook
eWorkbooks = get(e, 'Workbooks');
% neuWB=Add(eWorkbooks)
%%open a Workbook
neuWB=eWorkbooks.Open('YourExcelFile.xlsm');
Now you kann start your Macro in Excel
%%makro starten
e.ExecuteExcel4Macro(['!NameOfModule.NameOfSub(' INPUT_ValuesFromMatlabInExcel ')']);
%%save your work in excel
neuWB.Save();
neuWB.Saved = 1;
neuWB.Close;
%%excel clear and close
e.Quit;
e.delete
I hope that is what you need!
cheers Ahmad
2 个评论
Guillaume
2018-7-10
Note that this requires that the macro already exists in the workbook and that macros are enabled. In most recent versions of Excel, if you have the default security settings, unless the macro is signed, macros will be disabled.
I would not recommend lowering the security settings as macro viruses are still common.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!