[python-win32] COM problems

Tom Adelman tla@research.nj.nec.com
Tue, 24 Apr 2001 00:37:26 -0400


I was quite excited when I finally looked up what COM was supposed to do,
but have been falling flat in my attempts to use it.  It seems like a great
idea; do people generally have success with this for non-MS products?

I've been struggling for quite some time now trying to get Python COM to do
some simple tasks with a program called SigmaPlot.  I can get things
working in VB but not Python.  I'm using Windows NT, Python 1.5.2, win32all
build 136, and am trying to run SigmaPlot 6.1 as a client.  I convinced my
boss to upgrade SigmaPlot just so I could hook it up to Python, and then he
was going to start using Python once I did this.  So far I'm nowhere and
could really use some help -- any suggestions would be useful, I'm
completely stuck.  

Here's an example:

In VB (only the last two lines are really of interest here -- the other
lines work in both languages):

Dim sp As Object
Set sp = CreateObject("SigmaPlot.Application.1")
sp.Visible = True
sp.Notebooks.Add
sp.ActiveDocument.NotebookItems(2).DataTable.Cell(0, 0) = 5.2
sp.ActiveDocument.NotebookItems.Add (2)
TemplatePage = "Bar Chart for Slide"
sp.ActiveDocument.CurrentPageItem.ApplyPageTemplate (TemplatePage)

Yet in Python, this doesn't work and I get a traceback:

from win32com.client import Dispatch
sp = Dispatch("SigmaPlot.Application")
sp.Visible = 1
sp.Notebooks.Add
sp.ActiveDocument.NotebookItems(2).DataTable.Cell(0,0, 5.2)
sp.ActiveDocument.NotebookItems.Add(2)
TemplatePage = "Bar Chart for Slide"
sp.ActiveDocument.CurrentPageItem.ApplyPageTemplate(TemplatePage)

Traceback (innermost last):
  File "<string>", line 1, in ?
  File "D:\PythonCode\testSP10.py", line 53, in ?
    sp.ActiveDocument.CurrentPageItem.ApplyPageTemplate(TemplatePage)
  File "D:\Program Files\python\win32com\client\dynamic.py", line 394, in
__getattr__
    raise pythoncom.com_error, details
com_error: (-2147352567, 'Exception occurred.', (65535, 'SigmaPlot 6.1',
'No template name supplied.', None, 0, 0), None)

If you know what's wrong at this point, no need to read further (althoug
please tell me).  Otherwise, here are some possibly relevent clues:

1)  My guess is that PythonCOM is sending the command "ApplyPageTemplate"
without the parameter, and then when SigmaPlots gets this it balks and says
it needs the parameter.  This is consistent with the line
"sp.Notebooks.Add" working but "sp.Notebooks.Add()" not working.

2)  I've tried an analgous tests for Excel (a few examples from _Python
Programming on Win32_) and everything seems to work for Excel.

3)  Early binding doesn't get started (for SigmaPlot, it works for Excel),
even though I've run MakePy, and even if I start the above script with
gencache.EnsureModule('{6E73CA05-CE63-11CF-B59C-0000929132CE}', 0, 1, 0)
I'm a bit confused by all of this, but I've tried to trace it through the
modules:
The line in the function __WrapDispatch in win32com\client\__init__:
Typeinfo = dispatch.GetTypeInfo()
raises an exception, which then results in a dynamic dispatch.
Explicitely, the exception is (found by reraising it).

File "D:\Program Files\Python\win32com\client\__init__.py", line 28, in
__WrapDispatch
    typeinfo = dispatch.GetTypeInfo()
com_error: (-2147467263, 'Not implemented', None, None)

Maybe this is happening because pythoncom.connect was supposed to work and
it doesn't.  It raises the exception:

Traceback (innermost last):
  File "<string>", line 1, in ?
  File "D:\PythonCode\testSP12.py", line 12, in ?
    sp = win32com.client.Dispatch("SigmaPlot.Application")
  File "D:\Program Files\Python\win32com\client\__init__.py", line 97, in
Dispatch
    dispatch, userName =
dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
  File "D:\Program Files\Python\win32com\client\dynamic.py", line 84, in
_GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "D:\Program Files\Python\win32com\client\dynamic.py", line 70, in
_GetGoodDispatch
    IDispatch = pythoncom.connect(IDispatch)
com_error: (-2147221021, 'Operation unavailable', None, None)


At this point I'm completely lost.  I've spent many hours on this, and
although I would really like to get it running, I'm about to give up.  If
you've gotten this far, I really appriciate your taking the time to read my
ramblings and guesses.  And any help would be greatly appriciated.

Thanks, 
Tom
TA16@cornell.edu