Search code examples
pythonmatlabpowerpointactivexwin32com

MATLAB ActiveX vs. Python Win32COM


I am trying to migrate a program I created in Python to MATLAB. This application uses win32COM to write/read to/from Excel and PowerPoint and I assumed the ActiveX commands would be very similar/identical because it is based on VBA. If someone could shed light on this and help with the following error I would appreciate it!!

So I have a Python code which works

PPT_App = win32com.client.Dispatch("PowerPoint.Application")
Presentation = PPT_App.Presentations.Add()
Cover_Slide = Presentation.Slides.Add(1,12)

However when I convert that to MATLAB;

PPT_App = actxserver('PowerPoint.Application');
Presentation = PPT_App.Presentations.Add();
Cover_Slide = Presentation.Slides.Add(1,12);

it gives me this error on line 3 defining 'Cover_Slide';

Undefined function 'Add' for input arguments of type 'Interface.91493469_5A91_11CF_8700_00AA0060263B'.

Anyone have an idea why this is happening or where I could find info on ActiveX differences?


Solution

  • They're not based on VBA; rather, it's the other way around, and anything you can do in VBA, you can do with ActiveX (so that in particular, what you can do with VBA, you can also do with MATLAB), but the methods and properties exposed by the COM object are a superset of those (so that win32com gets you a bit further).

    What you're seeing here is that, for whatever reason, the Slides COM object has a method called Add that is not available through ActiveX. What you do have, though, is an almost synonymous method called AddSlide which you can use in MATLAB. The only difference between the two is in the second parameter; Add takes a value in the PpSlideLayout enum (with the value of 12 corresponding to a blank slide), where AddSlide expects a CustomLayout, which you can create through Presentation.SlideMaster. Concretely, since in my case a "blank slide" happens to be the seventh available layout (when counted in the "New slide" dropdown), what you can do is

    Presentation = PPT_App.Presentations.Add();
    Layout = Presentation.SlideMaster.CustomLayouts.Item(7)
    Presentation.Slides.AddSlide(1, Layout)
    

    In general, refer to the VBA documentation to see what is possible through MATLAB. In my experience, the discrepancy between this and the general COM object is limited.