python win32/excel problem

justin worrall justin.worrall at morganstanley.com
Fri Mar 12 16:14:38 EST 2004


Hi,
 
I have a very simple python win32 example (gleaned from the O'Reilly
book on Win32 programming for Python), of an Excel client with a COM
server. Essentially I have three VB buttons - start_server,
stop_server and test_server. The first two do the obvious, the last
takes inputs from two cells in the excel sheets, then passes them to
the python server which adds them together and returns the result an
Excel message dialog.
 
Everything works just fine, except when I make changes to the python
code. The changes get picked up in Excel, but only when I quit the
entire Excel application. Obviously the COM object is being cached
somewhere - I am guessing that setting "Server = Nothing" only
overrides the reference, rather than destroying the object instance.
 
Any ideas on how to get VB to pick up a new python instance without
quitting?
 
thanks,
 
Justin
 
<<python>>
 
import pythoncom
import win32com.server.register
 
class simpleServer:
   
    _public_methods_=['add']
    _reg_progid_="jhw.simpleServer"
    _reg_clsid_=str(pythoncom.CreateGuid())
 
    def add(self,a,b):
        return a+b
   
if __name__=='__main__':
    win32com.server.register.UseCommandLine(simpleServer)
 
<<VB>>
 
Public SimpleServer As Object

Sub start_server()
    On Error GoTo start_server_error
    If Not (SimpleServer Is Nothing) Then
        MsgBox "server already running"
        Exit Sub
    End If
    Set SimpleServer = CreateObject("jhw.simpleServer")
    MsgBox "server started"
    Exit Sub
start_server_error:
    MsgBox "error starting server"
    End
End Sub

Sub stop_server()
    If (SimpleServer Is Nothing) Then
        MsgBox "server not running"
        Exit Sub
    End If
    Set SimpleServer = Nothing
    MsgBox "server stopped"
End Sub

Sub test_server()
    On Error GoTo test_server_error
    Dim a, b, r As Double
    a = Range("a").Value
    b = Range("b").Value
    r = SimpleServer.Add(a, b)
    MsgBox "result = " & r
    Exit Sub
test_server_error:
    MsgBox "error testing server - it's probably not running"
    End
End Sub



More information about the Python-list mailing list