DDE Question

E

eshields200

I'm new to VBA programming and I'm trying to interface with an optical system
design program called Zemax. I've written the following bit of code:

Sub Zemax_Test()
channelNumber = Application.DDEInitiate("Zemax", "temp")
x = Application.DDERequest(channelNumber, "GetFile")
y = 1
Worksheets("Sheet1").Range("B1").Value = CStr(x)
Worksheets("Sheet1").Range("B2").Value = y
Worksheets("Sheet1").Range("B3").Value = channelNumber
Worksheets("Sheet1").Range("B4").Value = IsError(channelNumber)
Worksheets("Sheet1").Range("B5").Value = IsError(x)
Application.DDETerminate channelNumber
End Sub

The resulting changes are made to column B of the spreadsheet:
Error 2042
1
2
FALSE
TRUE

The following is from the Zemax manual and describes their DDE interface:
*****
Establishing the link
To establish a DDE link with ZEMAX, the client program must broadcast a
message to all top level windows that includes a reference to the application
name, and the topic name. For ZEMAX, the application name is "ZEMAX", and the
topic name can be any non-NULL string. ZEMAX does not use the topic name,
just the application name and the "item". The item indicates to ZEMAX what
data is being requested. The various items ZEMAX supports are described
later. Once the DDE link is established, any analysis that can be done based
upon ray tracing may be performed by the server, and the data returned to the
client.
Terminating the link
When all computations are complete, and the DDE link is to be terminated by
the client, the client sends a termination message to the ZEMAX server. This
message will terminate only the server portion of ZEMAX, not ZEMAX itself.
Extracting data from ZEMAX
ZEMAX supports a number of capabilities under DDE. Each function is given a
name, called the "item", and this item is passed to the ZEMAX server using
the WM_DDE_REQUEST message. Any data ZEMAX requires (for example to trace a
ray) is passed to ZEMAX by encoding within the item name. Most items in ZEMAX
have names that require no further encoding, but some items have coded values
appended to the name. The values are appended to the item name and are
delimited by commas.
ZEMAX responds to each item request with a WM_DDE_DATA message, and the
message contains a pointer to a block of memory holding a string. Most data
is passed from ZEMAX back to the client application in a string, this
corresponds to F_TEXT format. The client application must extract the string
and free the memory for the string.
*****

I have no problem getting the DDE to work from Matlab. Also, typing
"=Zemax|temp!GetFile" into a cell behaves properly.

Does anybody have any suggestions?

Thanks,
Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top