[python-win32] DDE: python server/excel client - pass array?

David Haas david.h.haas at gmail.com
Thu Dec 25 02:28:18 CET 2008


I've run into a problem with DDE which, after much googling and trial
& error, I managed to work around, but I'm pretty sure there's a
better solution.

I think the problem is this: there's currently no way for a python DDE
server to pass an array back as a response from a DDE Request - it's
either a string, or nothing.  Since Excel expects the response it
receives from a DDE Request that it makes to be an array, it dies
(with what I believe is an Error 2042) when communicating with a
Python DDE server.

If someone could give me some idea how to fix what I think is broken
in python's DDE module, I'd greatly appreciate it.  Even better, if
someone knows an easy way to do this which has totally escaped me, I'd
greatly appreciate knowing how to do it. And if nothing else, someone
else may google this response and save themselves some hassle.
Details of my setup & what I've tried is below:

I'm running Win XP, Python 2.6.1, pywin32 build 212 from Sourceforge,
MS Excel from Office 2000.

My python DDE server: basically taken verbatim from win32/demos/dde/ddeserver.py
I think the key piece of code is in the "MyRequestTopic" class -
namely, the call to CreateStringItem:

class MyRequestTopic(object.Object):
       def __init__(self, topicName):
               topic = dde.CreateTopic(topicName)
               topic.AddItem(dde.CreateStringItem(""))     <- this line
               object.Object.__init__(self, topic)

My Excel client is using a little bit of VBA to query the python DDE server:

dim chan as long
dim resp as variant

chan = DDEInitiate("pyserver","request")
resp = DDERequest(chan, "Hello")
DDETerminate chan

If I start my python DDE server, and use a python DDE client, passing
the above request will return a valid response from the system (right
now, it just echos back whatever it receives, so we'd get a response
of "Hello").
If I plug the above VBA code into Microsoft Word, the variable "resp"
in VBA gets set to "Hello", which is the appropriate response.  Word's
documentation says it expects a String as a response when it makes a
DDERequest.
However, running the above VBA code in Microsoft Excel will set the
variable "resp" to "Error 2042".  Actually, passing any valid request
will give me a response of "Error 2042" in Excel.  Interestingly
enough, the documentation for Excel says it expects an Array as a
response when it makes a DDERequest.

I've tried changing the response from the DDE server into (for
example) a list and/or a tuple, but that still gave me the same error
in Excel.  Interestingly enough, it also would then break in Word.

My workaround in Excel is to make a Word object, and then use the Word
object to handle all the requests from my python DDE server.  It
works, but it's not elegant.

Looking through the code of ddemodule.cpp, I see a method for
"CreateStringItem", but nothing like "CreateArrayItem", or
"CreateGenericItem" .  Looking at the documentation for TCL's DDE
implementation, it looks like it can pass back either a string or a
binary array.

Any ideas on either of the following two questions would be greatly appreciated:

a) how I could use the current dde module to pass in something Excel could read;
b) what extra code would need to be added to the dde module to pass back arrays;


Thanks . . .

- David


More information about the python-win32 mailing list