The Excel file ddeserv.xls contained in the DDE subdirectory demonstrates how DDE can be used to control and get values from Vensim. To use this example you must first start Vensim. The functions used to manage DDE are:
Dim DDE_channel As Integer
Sub StartupDDE()
DDE_channel = Application.DDEInitiate("VENSIM", "System")
End Sub
Starts Vensim - the topic name is arbitrary.
Sub LoadVensimModel()
varstr$ = "[SPECIAL>LOADMODEL|" + Cells(2, 7).Value + "]"
Application.DDEExecute DDE_channel, varstr$
End Sub
Loads the model name typed into the cell 2,7 (G2) using an execute command.
Sub SetConstant()
varstr$ = "[Simulate>SETVAL|" + Cells(5, 4).Text +"="+ Cells(5, 5).Text + "]"
Application.DDEExecute DDE_channel, varstr$
End Sub
Reads a constant name and value from the spreadsheet and uses SETVAL to set the value for it in Vensim.
Sub Simulate()
Application.DDEExecute DDE_channel, "[MENU>RUN1|O]"
End Sub
Simulates the model.
Sub GetValue()
Dim returnList As Variant
varstr$ = Cells(8, 4).Text + "@" + Cells(8, 5).Text
returnList = Application.DDERequest(DDE_channel, varstr$)
Cells(8, 6).Value = returnList(LBound(returnList))
End Sub
Gets a value for the variable named at the time specified in the spreadsheet.
Sub StopDDE()
Application.DDETerminate DDE_channel
End Sub
Stops the DDE communication.
If you try the spreadsheet you can press the buttons in order. You can change things and press intermediate buttons any number of times, but you should press the open and close communications only once. Depending on what you are doing you might want to open and close communications within each command as this is not that slow and will save you the trouble of remembering whether or not you have done so.