[python-win32] problems in returning parameters using a COM client

George Vestergom unifoam@total.net
Thu, 19 Dec 2002 13:23:37 -0500


Here's my problem:

I'm writing routines to access the methods found in CorelDraw 6. I've
used the MakePy utility to provide early-bound automation. Everything
works fine, except I can't return values from certain methods.

For example, one CorelDraw method returns 2 Long values: 
     .GetPosition lngXPos, lngYPos

     where lngXPos and lngYPos are the x and y position of the currently
selected object.

Using Python 2.2 and win32all build 148:

>>> from win32com.client import Dispatch
>>> cd = Dispatch('CorelDraw.automation.6')
>>> cd.Filenew()
>>> cd.SetVisible(CDTrue)

>>> ## everything ok here, CorelDraw starts up and a new drawing surface
>>> ## is made visible.

>>> ## now draw a rectangle using the following method:
>>> ## .CreateRectangle lngTop, lngLeft, lngBottom, lngRight, 0
>>> ### (numbers are in CorelDraw units: tenths of a micron)

>>> cd.CreateRectangle( 1500000, -1200000, 750000, -500000, 0)

>>> ## a rectangle is drawn and becomes the current selected object.
>>> ## now retrieve the current position of the rectangle:

>>> x, y = cd.GetPosition()  # it should return x = -1200000, y = 1500000

>>> ## the following error is printed:
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in ?
  File "<COMObject CorelDraw.automation.6>", line 2, in
com_error: (-2147352561, 'Parameter not optional.', None,

The following also do not work:
>>> (x, y) = cd.GetPosition()
>>> cd.GetPosition()
>>> print cd.GetPosition()

Here's an example using Excel 2000 VBA which works fine since the return
variables must be  defined:

Public Sub CorelDraw()
   Dim lngXPos As Long
   Dim lngYPos As Long

   ' create an automation object
   Set cd = CreateObject("CorelDraw.Automation.6")
   ' create a rectangle and return current x, y position
   With cd
     .SetVisible -1     
     .CreateRectangle 1500000, -1200000, 750000, -500000, 0
     .GetPosition lngXPos, lngYPos
     Debug.Print lngXPos, lngYPos
   End With

   Set cd = Nothing

End Sub

My question is this:

Is it possible to retrieve these values in another way, maybe using
PyIDispatch.InvokeTypes()? I can't see anything mentionned in Hammond's
& Robinson's "Python Programming on Win32" to resolve this problem. 

Here's the MakePy entry for this method:
     def GetPosition(self, XPos=defaultNamedNotOptArg,
         return self._oleobj_.InvokeTypes(0x11, LCID, 1, (11,0),((16387,
0), (16387, 0)),XPos, YPos)

One idea I have in mind is to create an COM object from Excel and have
python call a method in Excel which calls the GetPosition method in
CorelDraw and stores the results on a worksheet. Python can then
retrieve the values from this sheet. 

Any suggestions would be appreciated.

George Vestergom