How to run a Power Point Macro from MATLAB?

10 ビュー (過去 30 日間)
Alberto
Alberto 2019 年 5 月 20 日
編集済み: Guillaume 2019 年 9 月 12 日
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?

採用された回答

Guillaume
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
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
Guillaume
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 ExchangeMATLAB Report Generator についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by