Running macro within vba project from Matlab

5 views (last 30 days)
Aadil
Aadil on 22 Aug 2012
Answered: Aniket on 8 Apr 2025
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,

Answers (1)

Aniket
Aniket on 8 Apr 2025
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.

Community Treasure Hunt

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

Start Hunting!