I have a very simple model that I'm trying to run from Excel. I can get the values of the variables from Vensim to Excel without any problem but for some reason I cannot pass a new value to a Vensim variable from Excel. The variable is defined in Vensim as a Gaming variable. Here is the code I use.
Code: Select all
Option Explicit
Private Declare Function vensim_be_quiet Lib "vendll32.dll" (ByVal quietflag As Long) As Long
Private Declare Function vensim_check_status Lib "vendll32.dll" () As Long
Private Declare Function vensim_command Lib "vendll32.dll" (ByVal Vcommand$) As Long
Private Declare Function vensim_continue_simulation Lib "vendll32.dll" (ByVal number_time_step As Long) As Long
Private Declare Function vensim_finish_simulation Lib "vendll32.dll" () As Long
Private Declare Function vensim_get_data Lib "vendll32.dll" (ByVal filename$, ByVal varname$, ByVal timename$, varvals As Single, timevals As Single, ByVal maxpoints As Integer) As Long
Private Declare Function vensim_get_dpval Lib "vendll32.dll" (ByVal varname$, varval As Double) As Long
Private Declare Function vensim_get_dpvecvals Lib "vendll32.dll" (vecoff As Long, varvals As Double, ByVal veclen As Long) As Long
Private Declare Function vensim_get_info Lib "vendll32.dll" (ByVal infowanted As Long, ByVal buf$, ByVal maxbuflen As Long) As Long
Private Declare Function vensim_get_sens_at_time Lib "vendll32.dll" (ByVal filename$, ByVal varname$, ByVal timename$, attime As Single, vals As Single, ByVal maxpoint As Long) As Long
Private Declare Function vensim_get_substring Lib "vendll32.dll" (ByVal fullstring$, ByVal frompos As Long, ByVal buf$, ByVal maxbuflen As Long) As Long
Private Declare Function vensim_get_val Lib "vendll32.dll" (ByVal varname$, varval As Single) As Integer
Private Declare Function vensim_get_varattrib Lib "vendll32.dll" (ByVal varname$, ByVal attrib As Long, ByVal buf$, ByVal maxbuflen As Long) As Long
Private Declare Function vensim_get_varnames Lib "vendll32.dll" (ByVal filter$, ByVal vartype As Long, ByVal buf$, ByVal maxbuflen As Long) As Long
Private Declare Function vensim_get_varoff Lib "vendll32.dll" (ByVal varname$) As Long
Private Declare Function vensim_get_vecvals Lib "vendll32.dll" (vecoff As Long, varvals As Single, ByVal nelm As Long) As Long
Private Declare Function vensim_set_parent_window Lib "vendll32.dll" (ByVal vwidnow As Long, ByVal r1 As Long, ByVal r2 As Long) As Long
Private Declare Function vensim_show_sketch Lib "vendll32.dll" (ByVal viewnum As Long, ByVal wantscroll As Long, ByVal zoompercent As Long, ByVal Vwindow As Long) As Long
Private Declare Function vensim_start_simulation Lib "vendll32.dll" (ByVal loadfirst As Integer, ByVal game As Long, ByVal overwrite As Long) As Long
Private Declare Function vensim_tool_command Lib "vendll32.dll" (ByVal Vcommand$, ByVal Vwindow As Long, ByVal iswip As Long) As Long
Sub simulate_demand()
Dim result As Integer
Dim varstr As String
Dim rval1(10) As Single
Dim rval2(10) As Single
Dim rval3(10) As Single
Dim rval4(10) As Single
Dim rval5(10) As Single
Dim rval6(10) As Single
Dim tval(10) As Single
Dim num As Single
Dim i As Single
Dim j As Single
result = vensim_be_quiet(2)
varstr = "SPECIAL>LOADMODEL|" & ThisWorkbook.Path & "\ModelB.vpm"
result = vensim_command(varstr)
result = vensim_command("SIMULATE>CHGFILE")
result = vensim_command("SIMULATE>DATA")
result = vensim_command("SIMULATE>RUNNAME|game")
result = vensim_command("MENU>GAME")
result = vensim_command("SIMULATE>SETVAL|ludo = 3")
MsgBox result 'This returns 1
result = vensim_get_data("Current2.vdf", "olive", "Time", rval4(1), tval(1), 10)
For i = 1 To result
Sheet3.Range("K3").Offset(i, 0) = tval(i) 'Time
Sheet3.Range("L3").Offset(i, 0) = rval4(i) 'Olive
Next i
result = vensim_get_data("Current2.vdf", "ludo", "Time", rval5(1), tval(1), 10)
For i = 1 To result
Sheet3.Range("N3").Offset(i, 0) = tval(i) 'Time
Sheet3.Range("O3").Offset(i, 0) = rval5(i) 'ludo
Next i
result = vensim_get_data("Current2.vdf", "toto", "Time", rval6(1), tval(1), 10)
For i = 1 To result
Sheet3.Range("Q3").Offset(i, 0) = tval(i) 'Time
Sheet3.Range("R3").Offset(i, 0) = rval6(i) 'toto
Next i
result = vensim_command("GAME>ENDGAME")
End Sub
Any help on this would be greatly appreciated

Thank you very much for your time.