[python-win32] dynamic Word strangeness

Robin Becker robin at reportlab.com
Fri Apr 22 15:24:18 CEST 2005


...I've started getting problems with dynamic dispatch

eg

PythonWin 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32.
Portions Copyright 1994-2004 Mark Hammond (mhammond at skippinet.com.au) - see 
'Help/About PythonWin' for further copyright information.
 >>> import win32com.client.dynamic
 >>> word = win32com.client.dynamic.Dispatch("Word.Application")
Traceback (most recent call last):
   File "<interactive input>", line 1, in ?
   File "C:\Python\Lib\site-packages\win32com\client\dynamic.py", line 98, in 
Dispatch
     IDispatch, userName = _GetGoodDispatchAndUserName(IDispatch,userName,clsctx)
   File "C:\Python\Lib\site-packages\win32com\client\dynamic.py", line 91, in 
_GetGoodDispatchAndUserName
     return (_GetGoodDispatch(IDispatch, clsctx), userName)
   File "C:\Python\Lib\site-packages\win32com\client\dynamic.py", line 79, in 
_GetGoodDispatch
     IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, 
pythoncom.IID_IDispatch)
com_error: (-2147024770, 'The specified module could not be found.', None, None)
 >>>

I'm running xp sp2 with word 2003 (I think it's version 11). The code I'm using 
is based entirely on the test stuff.

When I run the test I see the following

C:\Python\Lib\site-packages\win32com\test>testMSOffice.py
Starting Word 8 for dynamic test
Starting Word 7 for dynamic test
Traceback (most recent call last):
   File "C:\Python\Lib\site-packages\win32com\test\testMSOffice.py", line 138, 
in TestAll
     TestWord()
   File "C:\Python\Lib\site-packages\win32com\test\testMSOffice.py", line 41, in 
TestWord
     TestWord7(word)
   File "C:\Python\Lib\site-packages\win32com\test\testMSOffice.py", line 49, in 
TestWord7
     word.FileNew()
TypeError: 'NoneType' object is not callable

C:\Python\Lib\site-packages\win32com\test>testMSOffice.py
Starting Word 8 for dynamic test
Starting Word 8 for non-lazy dynamic test
Starting Word 7 for dynamic test
Traceback (most recent call last):
   File "C:\Python\Lib\site-packages\win32com\test\testMSOffice.py", line 138, 
in TestAll
     TestWord()
   File "C:\Python\Lib\site-packages\win32com\test\testMSOffice.py", line 41, in 
TestWord
     TestWord7(word)
   File "C:\Python\Lib\site-packages\win32com\test\testMSOffice.py", line 49, in 
TestWord7
     word.FileNew()
TypeError: 'NoneType' object is not callable

nothing happens in the first run, but the second does open word and do the 
writes etc. After I have done the tesMSOffice I see that

 >>> word = win32com.client.dynamic.Dispatch("Word.Application")
 >>>

works as expected. Is word non-dynamic now?

As an extra nastiness my test code also works after the testMSOffice run, but 
seems to disable it for the next run. My test looks like

def doWordDoc(wd):
	wd.Visible=1
	doc=wd.Documents.Add()
	wrange=doc.Range()
	for i in xrange(10):
		wrange.InsertAfter("Hello from Python %d\n" % i)
	paras = doc.Paragraphs
	for i in xrange(len(paras)):
		p = paras[i]()
		p.Font.ColorIndex = i+1
		p.Font.Size = 12 + (4 * i)
	del wrange, paras, p
	doc.Close(SaveChanges = 0)
	wd.Quit()
	del doc

and is missing the win32api.sleep(1000).

Any ideas?
-- 
Robin Becker


More information about the Python-win32 mailing list