python win32/excel problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • justin worrall

    python win32/excel problem

    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_="j hw.simpleServer "
        _reg_clsid_=str (pythoncom.Crea teGuid())
     
        def add(self,a,b):
            return a+b
       
    if __name__=='__ma in__':
        win32com.server .register.UseCo mmandLine(simpl eServer)
     
    <<VB>>
     
    Public SimpleServer As Object

    Sub start_server()
        On Error GoTo start_server_er ror
        If Not (SimpleServer Is Nothing) Then
            MsgBox "server already running"
            Exit Sub
        End If
        Set SimpleServer = CreateObject("j hw.simpleServer ")
        MsgBox "server started"
        Exit Sub
    start_server_er ror:
        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_err or
        Dim a, b, r As Double
        a = Range("a").Valu e
        b = Range("b").Valu e
        r = SimpleServer.Ad d(a, b)
        MsgBox "result = " & r
        Exit Sub
    test_server_err or:
        MsgBox "error testing server - it's probably not running"
        End
    End Sub
Working...