从MATLAB复制“ ThisWorkbook”中Excel文件中的宏

时间:2018-08-21 14:54:30

标签: excel vba excel-vba matlab

我在文本文件(Macro.txt)中写了一个宏,而我有一个Excel文件(Result.xlsm)。我需要从MATLAB中复制“ ThisWorkbook”中Excel文件中的宏(下图)。我使用import函数尝试了以下代码。但是它将宏作为模块编写。我想知道是否有什么方法可以在“ ThisWorkbook”中编写宏?

[The place that I need to copy the macro code]

ResultFileName = [pwd() filesep 'Result.xlsm'];
MacroFileName = [pwd() filesep 'Macro.txt'];

% Import the excel file
Excel= actxserver('Excel.Application');
wb=Excel.Workbooks.Open(ResultFileName);
Excel.visible = 1;

% Add the macro to the excel file
import(wb.VBProject.VBComponents,MacroFileName);

% Save
Excel.Application.DisplayAlerts = 0; %Avoid overwrite warning
SaveAs(wb,ResultFileName);
Excel.Application.DisplayAlerts = 1;

% Close Excel
Quit(Excel);
delete(Excel);

0 个答案:

没有答案