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