[IronPython] Word Automation

Martin Maly martmaly at exchange.microsoft.com
Mon Jun 20 07:34:39 CEST 2005


Tim already answered the first problem you faced, let's see if I can
help with the others.

>> I do not understand why I cannot iterate in the collection of

While the collection you get back may (and I am not sure whether it does
or not) inherit from IEnumerable,
the code I put in IronPython doesn't detect it yet so we cannot iterate
it the tradiional Pythonic way. Anthony
Tarlano and I played with this already and we have been successful using
methods Count and Item on the 
collection (the collection we enumerated was collection of spelling
correction hints):

>>> import sys
>>> sys.LoadAssemblyByName('Microsoft.Office.Interop.Word')
>>> import Microsoft.Office.Interop.Word as Word
>>> wapp = Word.ApplicationClass()
>>> wapp.Documents.Add()
<com_object Microsoft.Office.Interop.Word.DocumentClass>
>>> s = wapp.GetSpellingSuggestions('tonny')

>>> for i in xrange(s.Count):
...     print s.Item(i + 1).Name


Same goes for collection of documents:

>>> import sys
>>> sys.LoadAssemblyByName("Microsoft.Office.Interop.Word")
>>> import Microsoft.Office.Interop.Word as wordApp
>>> mApp = wordApp.ApplicationClass()
>>> mApp.Visible=True
>>> mApp.Documents.Open("D:\\a.doc")
>>> mApp.Documents.Add()
>>> for i in xrange(mApp.Documents.Count):
...     print mApp.Documents.Item(i + 1).Name
>>  I cannot select a given document using its Name 

I was able to access the document by name using slightly modified code:

>>> print mApp.Documents.Item("X.doc").Name

>> I would like to translate the following vb statement in python
>> Documents.Open FileName:="C:\Files\Doc.doc", ReadOnly:=True

>>> doc3 = mApp.Documents.Open(FileName='c:\tmp\Doc1.doc', ReadOnly=

Passing parameters by name to COM objects is not implemented in

To open document read only, you can do following:

 mApp.Documents.Open("D:\\x.doc", False, True)

The 3rd parameter is "Read Only". The rest of the parameters have
default values,
which IronPython will add.

The COM support in IronPython is going to be extended to get the best
possible experience. Currently the support is only very simple.

I hope this helps a little.


More information about the Ironpython-users mailing list