Running macro within vba project from Matlab

4 vues (au cours des 30 derniers jours)
Aadil
Aadil le 22 Août 2012
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,

Réponses (1)

Aniket
Aniket le 8 Avr 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.

Catégories

En savoir plus sur Use COM Objects in MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by