[IronPython] i thought this is odd: using Interop on Microsoft Word
Shri Borde
Shri.Borde at microsoft.com
Fri Nov 2 09:14:22 CET 2007
This could be fixed in IronPython but currently its expected.
docs[1] is calling into this indexer in Microsoft.Office.Interop.Word.dll. The indexer method takes the index by-reference. Ie as "object&" instead of just "object".
.method instance class Microsoft.Office.Interop.Word.Document
get_Item([in] object& marshal( struct) Index)
{
} // end of method Documents::get_Item
IronPython thinks that is a ref parameter that could potentially by updated by the call. Hence, it returns it back as one of the return values.
>>> returnValues = docs[1]
>>> returnValues[0] # real return value
Microsoft.Office.Interop.Word.DocumentClass
>>> returnValues[1] # the by-ref param included in the return value.
1
>>>
In C#, you would have to use the "ref" keyword to index into docs. You can simulate this in IronPython as such
>>> docs[clr.Reference[object](1)]
Microsoft.Office.Interop.Word.DocumentClass
>>>
IronPython could fix this by paying attention to the "[in]" in the signature. It appears as a custom attribute of type System.Runtime.InteropServices.InAttribute on the parameter. I have opened http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=13641 to track this.
-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Kelie
Sent: Friday, November 02, 2007 12:45 AM
To: IronPython Users
Subject: [IronPython] i thought this is odd: using Interop on Microsoft Word
Hello group,
import clr
clr.AddReference("Microsoft.Office.Interop.Word")
import Microsoft.Office.Interop.Word as Word
_progid_ = "Word.Application"
def get_active_object(progid):
from System.Runtime.InteropServices import Marshal
try:
app = Marshal.GetActiveObject(progid)
except:
app = None
return app
def get_active_application():
return get_active_object(_progid_)
def get_documents():
return get_active_application().Documents
if __name__ == '__main__':
docs = get_documents()
# print docs[1].FullName # This line causes AttributeError:
# 'tuple' object has no attribute 'FullName'
print docs[1] # returns (Microsoft.Office.Interop.Word.DocumentClass, 1)
print docs[2] # returns (Microsoft.Office.Interop.Word.DocumentClass, 2)
print docs[1][0].FullName
This is the code that i tested. For the last couple of lines, i was
expecting that
docs[1] and docs[2] would return the document object instead of a
tuple with the first of its item being the document. Is this the
intended behavior?
Thank you,
--
Kelie
_______________________________________________
Users mailing list
Users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
More information about the Ironpython-users
mailing list