Execute TI Process from VBA
Posted: Tue Mar 02, 2010 11:07 pm
HI All,
I have the following snippet of code that I found on this board, however it is not running the process, I am getting a successful connect though.
Could anybody shed some light as to what I'm doing wrong?
Option Explicit
Global arr()
Declare Function TM1_API2HAN Lib "tm1.xll" () As Long
Sub tm1EP()
Dim hUser As Long
Dim hPool As Long
Dim hServer As Long
Dim vServerName As Long
Dim vClientName As Long
Dim vClientPassword As Long
Dim ServerName As String * 75
Dim ClientName As String * 75
Dim ClientPassword As String * 75
Dim ErrorString As String * 75
Dim ServerHandle As Long, ProcessHandle As Long
Dim TotParams As Long
Dim InitParamArray() As Long, ParamArrayHandle As Long
Dim ExecuteResult As Long
Dim SessionHandle As Long
Dim ProcessParameters()
Dim i
' initialize the API
tm1api.TM1APIInitialize
hUser = TM1SystemOpen()
'Set the Admin Host Server Name
TM1SystemAdminHostSet hUser, "tm1qa.test"
' Create a Pool Handle
hPool = TM1ValPoolCreate(hUser)
' Establish Login information
ServerName = "tm1qa"
ClientName = "admin"
ClientPassword = "test"
vServerName = TM1ValString(hPool, Trim(ServerName), 0)
vClientName = TM1ValString(hPool, Trim(ClientName), 0)
vClientPassword = TM1ValString(hPool, Trim(ClientPassword), 0)
'Log in to a TM1 Server
hServer = TM1SystemServerConnect(hPool, vServerName, vClientName, vClientPassword)
' Check to see if we were successful...
If (TM1ValType(hUser, hServer) = TM1ValTypeObject()) Then MsgBox "You Logged in Successfully"
If (TM1ValType(hUser, hServer) = TM1ValTypeError()) Then MsgBox "The server handle contains an error code."
'TM1ValErrorString_VB hUser, hServer, ErrorString, 0
SessionHandle = GetExcelSessionHandle()
If SessionHandle = 0 Then
MsgBox "ERROR: Cannot communicate with TM1 API."
Exit Sub
End If
ProcessHandle = TM1ObjectListHandleByNameGet(hPool, hServer, TM1ServerProcesses, TM1ValString(hPool, "AutolockPeriod", 0))
' Get parameters, build array of param & type
TotParams = -1 'UBound(ProcessParameters)
If TotParams < 0 Then 'No parameters passed
ReDim InitParamArray(1)
InitParamArray(1) = TM1ValString(hPool, "", 1)
ParamArrayHandle = TM1ValArray(hPool, InitParamArray, 0)
Else
ReDim RawParameterArray(TotParams, 1)
'Collect the params, get their type
For i = 0 To TotParams
RawParameterArray(i, 0) = ProcessParameters(i)
If VarType(ProcessParameters(i)) = vbString Then
RawParameterArray(i, 1) = "S"
Else
RawParameterArray(i, 1) = "N"
End If
Next i
'Now array of handles to params for process
ReDim InitParamArray(TotParams)
For i = 0 To TotParams
If RawParameterArray(i, 1) = "S" Then
InitParamArray(i) = TM1ValString(hPool, CStr(RawParameterArray(i, 0)), 0)
Else
InitParamArray(i) = TM1ValReal(hPool, CDbl(RawParameterArray(i, 0)))
End If
Next i
'Set the parameters in TM1 array
ParamArrayHandle = TM1ValArray(hPool, InitParamArray, TotParams + 1)
For i = 0 To TotParams
TM1ValArraySet ParamArrayHandle, InitParamArray(i), i + 1
Next i
End If
'Execute process
ExecuteResult = TM1ProcessExecuteEx(hPool, ProcessHandle, ParamArrayHandle)
tm1api.TM1APIFinalize
Cleanup_API:
TM1ValPoolDestroy hPool
End Sub
I have the following snippet of code that I found on this board, however it is not running the process, I am getting a successful connect though.
Could anybody shed some light as to what I'm doing wrong?
Option Explicit
Global arr()
Declare Function TM1_API2HAN Lib "tm1.xll" () As Long
Sub tm1EP()
Dim hUser As Long
Dim hPool As Long
Dim hServer As Long
Dim vServerName As Long
Dim vClientName As Long
Dim vClientPassword As Long
Dim ServerName As String * 75
Dim ClientName As String * 75
Dim ClientPassword As String * 75
Dim ErrorString As String * 75
Dim ServerHandle As Long, ProcessHandle As Long
Dim TotParams As Long
Dim InitParamArray() As Long, ParamArrayHandle As Long
Dim ExecuteResult As Long
Dim SessionHandle As Long
Dim ProcessParameters()
Dim i
' initialize the API
tm1api.TM1APIInitialize
hUser = TM1SystemOpen()
'Set the Admin Host Server Name
TM1SystemAdminHostSet hUser, "tm1qa.test"
' Create a Pool Handle
hPool = TM1ValPoolCreate(hUser)
' Establish Login information
ServerName = "tm1qa"
ClientName = "admin"
ClientPassword = "test"
vServerName = TM1ValString(hPool, Trim(ServerName), 0)
vClientName = TM1ValString(hPool, Trim(ClientName), 0)
vClientPassword = TM1ValString(hPool, Trim(ClientPassword), 0)
'Log in to a TM1 Server
hServer = TM1SystemServerConnect(hPool, vServerName, vClientName, vClientPassword)
' Check to see if we were successful...
If (TM1ValType(hUser, hServer) = TM1ValTypeObject()) Then MsgBox "You Logged in Successfully"
If (TM1ValType(hUser, hServer) = TM1ValTypeError()) Then MsgBox "The server handle contains an error code."
'TM1ValErrorString_VB hUser, hServer, ErrorString, 0
SessionHandle = GetExcelSessionHandle()
If SessionHandle = 0 Then
MsgBox "ERROR: Cannot communicate with TM1 API."
Exit Sub
End If
ProcessHandle = TM1ObjectListHandleByNameGet(hPool, hServer, TM1ServerProcesses, TM1ValString(hPool, "AutolockPeriod", 0))
' Get parameters, build array of param & type
TotParams = -1 'UBound(ProcessParameters)
If TotParams < 0 Then 'No parameters passed
ReDim InitParamArray(1)
InitParamArray(1) = TM1ValString(hPool, "", 1)
ParamArrayHandle = TM1ValArray(hPool, InitParamArray, 0)
Else
ReDim RawParameterArray(TotParams, 1)
'Collect the params, get their type
For i = 0 To TotParams
RawParameterArray(i, 0) = ProcessParameters(i)
If VarType(ProcessParameters(i)) = vbString Then
RawParameterArray(i, 1) = "S"
Else
RawParameterArray(i, 1) = "N"
End If
Next i
'Now array of handles to params for process
ReDim InitParamArray(TotParams)
For i = 0 To TotParams
If RawParameterArray(i, 1) = "S" Then
InitParamArray(i) = TM1ValString(hPool, CStr(RawParameterArray(i, 0)), 0)
Else
InitParamArray(i) = TM1ValReal(hPool, CDbl(RawParameterArray(i, 0)))
End If
Next i
'Set the parameters in TM1 array
ParamArrayHandle = TM1ValArray(hPool, InitParamArray, TotParams + 1)
For i = 0 To TotParams
TM1ValArraySet ParamArrayHandle, InitParamArray(i), i + 1
Next i
End If
'Execute process
ExecuteResult = TM1ProcessExecuteEx(hPool, ProcessHandle, ParamArrayHandle)
tm1api.TM1APIFinalize
Cleanup_API:
TM1ValPoolDestroy hPool
End Sub