Running macro within vba project from Matlab

6 ビュー (過去 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 Center および File ExchangeUse 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