[IronPython] PowerPoint Automation

Martin Maly martmaly at exchange.microsoft.com
Thu Jun 30 23:28:20 CEST 2005


Hi,

As for the "Visible" property. Reflecting on the Office Primary Interop
Assemblies shows that PowerPoint's Visible property has different type
than Word's one. Word is Boolean whereas PowerPoint has MsoTriState enum
as type.

It also seems that hiding the application window once visible is not
possible, as you have discovered. This seems to be limitation of
PowerPoint itself. I tried both setting the Visible to False, and also
Toggle (another value from the MsoTriState enum) but I failed on all
accounts.

To work with slide ranges, using Python list won't work. Unfortunatelly,
there is no default conversion from the Python list data type to .NET
array type(s). However, you can use the .NET arrays directly. The
modified code would look as follows:

>>> import sys
>>> sys.LoadAssemblyByName("Microsoft.Office.Interop.PowerPoint")
>>> import Microsoft.Office.Interop.PowerPoint as Ppt
>>> app = Ppt.ApplicationClass()
>>> app.Visible = True
>>> app.Presentations.Open(r"C:\Documents and Settings\martmaly\My
Documents\IronPython.ppt")
>>> r=System.Array.CreateInstance(System.Int32, 5)
>>> r[0]=1
>>> r[1]=4
>>> r[2]=7
>>> r[3]=9
>>> r[4]=12
>>> range = app.Presentations.Item("IronPython.ppt").Slides.Range(r)
>>> range.Count
5

Using pure COM application from IronPython is possible, but to get .NET
interop to work, you will need to produce the .NET type information for
the COM library (provided the COM binary contains type library). You can
use "tlbimp" tool which is part of .NET SDK distribution to convert the
COM type library into .NET assembly.

Hope this helps
Martin


> kbond Wrote:
>
> Hello,
> 
> I am still playing aound with the office Automation the last 
> I try was PowerPoint and I was disapointed since I was 
> willing to migrate a macro I have done a long time ago. This 
> macro create a TOC to a ppt file.
> 
> 
> 
> >>>>>>Import sys
> >>>>>> sys.LoadAssemblyByName("Microsoft.Office.Interop.PowerPoint")
> >>>>>> import Microsoft.Office.Interop.PowerPoint as msPPT  pptApp = 
> >>>>>>msPPT.ApplicationClass()  pptApp.Visible
> >>>      
> >>>
> -1
> 
> ###### The value of this attribute is strange since for word 
> it is a boulean############""
> 
> 
> >>>>>> pptApp.Visible = True  # is working fine pptApp.Visible = False
> >>>      
> >>>
> System.Runtime.InteropServices.COMException: 
> Application.Visible : Invalid reque st.  Hiding the 
> application window is not allowed.
>    at input_12.Run(Frame frame)
>  #### I am know inable to hide the PowerPoint application ########
> 
> >>>>>>
> >>>      
> >>>
> pptApp.Presentations.Open(r"C:\tmp\testAutomation\tocPowerpoin
> t\testMerge.ppt")
> 
> 
> >>>>>> listOfSlide=[1,4]
> >>>>>> 
> pptApp.Presentations.Item("testMerge.ppt").Slides.Range(listOfSli
> >>>>>> de)
> >>>      
> >>>
> System.ArgumentException: Slides.Range : Illegal value for 
> ^0. Bad type: expecte d 1D array of Variants, Integers, 
> Longs, or Strings
>    at input_31.Run(Frame frame)
> #### In order to specify a range I would expect to be able to 
> give a list intead of an Array, am I wrong?###### Could you 
> please advise on how I can select a range of slides?
> 
> Is it possible to import a pure COM application with ironPython?
> Like I am doing using win32com.client.
> 
> Thank you again for ironPython.



More information about the Ironpython-users mailing list