Running macro within vba project from Matlab

조회 수: 7 (최근 30일)
Aadil
Aadil 2012년 8월 22일
답변: Aniket 2025년 4월 8일
Hi, I'm using this script in matlab, it works perfect:
excelObject = actxserver('Excel.Application');
excelObject.Workbooks.Open('C:\Test.xls');
excelObject.Run('Module1');
Now this has the macro saved inside the workbook so it can easily find the macro and run it.
I have a 'excel add-in' which is stored in the add ins section of Office, it contains a macro that I want to run using matlab. How can I refer to this macro because whenever I run it matlab says it cannot find the macro
Thanks,

답변 (1개)

Aniket
Aniket 2025년 4월 8일
This is a common issue when working with Excel add-ins (.xlam files) from MATLAB.
MATLAB interacts with Excel using the COM interface. When you open a workbook that contains a macro, it's scoped and active, so Module1.MacroName is enough. But with an add-in:
  • It may not be loaded yet.
  • Even if installed, it may not be loaded in the Excel instance that MATLAB created.
  • Macros from add-ins require fully qualified names, including the filename.
Please use the below updated code for using add-ins:
excel = actxserver('Excel.Application');
addinPath = 'C:\MyAddIns\MyAddin.xlam';
excel.Workbooks.Open(addinPath); % Open it just like a workbook
% Run your macro — use the fully-qualified macro name
% Format: 'MyAddin.xlam!ModuleName.MacroName'
% e.g., If inside Module1, you have a macro called HelloWorld
excel.Run('MyAddin.xlam!Module1.HelloWorld');
Note: Excel uses the filename of the add-in (not display name) in the macro reference.

카테고리

Help CenterFile Exchange에서 Use COM Objects in MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by