How to run a Power Point Macro from MATLAB?
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Guillaume
2019-5-20
编辑:Guillaume
2019-5-20
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 个评论
Michael
2019-9-12
编辑:Michael
2019-9-12
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238082/2019-09-12%2017_43_29-Microsoft%20Visual%20Basic%20for%20Applications%20-%20init.potm%20[Unterbrechen]%20-%20[Modul1%20(Co.png)
Guillaume
2019-9-12
编辑:Guillaume
2019-9-12
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Report Generator 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!