How to run a Power Point Macro from MATLAB?

10 visualizzazioni (ultimi 30 giorni)
I have a pre-existing Power-Point which contains a macro that I would like to run from my matlab program. I am trying to find an analogous command in Power-Point to one that I once used when callling a macro in excel, 'ExecuteExcel4Macro'. Are there any recommendations?

Risposta accettata

Guillaume
Guillaume il 20 Mag 2019
Modificato: Guillaume il 20 Mag 2019
It should be as simple as:
powerpoint = actxserver('Powerpoint.Application');
%if you want to see the powerpoint window:
%powerpoint.Visible = true;
presentation = powerpoint.Presentations.Open('C:\somewhere\somepptfile.ppt');
powerpoint.Run('NameOfTheMacro')
%... later on
powerpoint.Quit;
  7 Commenti
Michael
Michael il 12 Set 2019
Modificato: Michael il 12 Set 2019
Thanks for the tip containing FileName and module:
powerpoint.Run('FileName.pptm!modulename.Add2PowerPoint', [])
I also found out how to pass an argument, easy as this:
powerpoint.Run('FileName.pptm!modulename.Add2PowerPoint', 'arg')
But does anyone know how to pass multiple arguments to a function? I tried cell arrays, conventional arrays, different data types and of course arguments just separated by commas. Thanks! Do I need to specify certain VBA variable types within the function maybe?
Ok. Answering my own question ;-):
You can using either arrays or cell arrays:
powerpoint.Run('FileName.pptm!modulename.Add2PowerPoint', [1 2 3])
powerpoint.Run('FileName.pptm!modulename.Add2PowerPoint', {'f', 'o', 'o'})
But note that in VBA you will always have to treat the array (of type VARIANT!) as two-dimensional:
2019-09-12 17_43_29-Microsoft Visual Basic for Applications - init.potm [Unterbrechen] - [Modul1 (Co.png
Guillaume
Guillaume il 12 Set 2019
Modificato: Guillaume il 12 Set 2019
It should be a cell array. However, here you can run into problems as matlab doesn't always do the right conversion for SAFEARRAY inputs.
You may also have to tweak some not very well documented options (they're indirectly documented here), such as:
feature('COM_SafeArraySingleDim', 1)"
or
feature('COM_PassSafeArrayByRef', 1)
Unfortunately, if you encounter errors it's very difficult to find out what the problem (wrong option? wrong packing? wrong type?).
An option would be to use the .Net interface of powerpoint (Microsoft.Office.Interop.PowerPoint) instead of the COM interface. Matlab's .Net interface is more powerful and saner.
I'm in the process of installing R2019b, so can't test anything right now, but it should be something like:
NET.addAssembly('Microsoft.Office.Interop.PowerPoint.dll'); %only once per session
powerpoint = Microsoft.Office.Interop.PowerPoint.Application;
args = NET.createArray('System.Object',3); %for 3 arguments to pass to run
args(1) = 'something';
args(2) = 123.456;
args(3) = 789;
powerpoint.Run('FileName.pptm!modulename.Add2PowerPoint', args);
edit: since you edited your comment while I was writing mine:
So, yes cell array for mixed type, or plain matrix for a safearray of numbers. "But note that in VBA you will always have to treat the array". The first feature I mention above will pass a vector as a 1D array to your VBA code.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB Report Generator in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by