By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,828 Members | 1,862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,828 IT Pros & Developers. It's quick & easy.

python win32/excel problem

P: n/a
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(simpleServ er)
*
<<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
Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.