VBA Process Run Issue Encountered

Post Reply
User avatar
jim wood
Site Admin
Posts: 3953
Joined: Wed May 14, 2008 1:51 pm
OLAP Product: TM1
Version: PA 2.0.7
Excel Version: Office 365
Location: 37 East 18th Street New York
Contact:

VBA Process Run Issue Encountered

Post by jim wood »

Guys,

I have used soem of the code that I found on the forum to try run a process from Excel. I have been happliy plugging away but I have now come across an erorr that I can't find. I'm getting a type mismatch. Can any of you guys see where my problem is?

Code: Select all

Public Const c_ServerName = "tm1development"
Public Const c_AdminHost = "sjmb089as"
Declare Sub TM1APIInitialize Lib "tm1api.dll" ()
Declare Function TM1SystemOpen Lib "tm1api.dll" () As Long
Declare Function TM1ValPoolCreate Lib "tm1api.dll" (ByVal hUser As Long) As Long
Declare Sub TM1SystemAdminHostSet Lib "tm1api.dll" (ByVal hUser As Long, ByVal AdminHosts As String)
Declare Function TM1SystemServerNof Lib "tm1api.dll" (ByVal hUser As Long) As Integer
Declare Function TM1ValString Lib "tm1api.dll" (ByVal hPool As Long, ByVal InitString As String, ByVal MaxSize As Long) As Long
Declare Function TM1SystemServerConnect Lib "tm1api.dll" (ByVal hPool As Long, ByVal sDatabase As Long, ByVal sClient As Long, ByVal sPassword As Long) As Long
Declare Function TM1ValArray Lib "tm1api.dll" (ByVal hPool As Long, ByRef sArray() As Long, ByVal MaxSize As Long) As Long
Declare Sub TM1ValArraySet Lib "tm1api.dll" (ByVal vArray As Long, ByVal val As Long, ByVal index As Long)
Declare Function TM1ObjectListHandleByNameGet Lib "tm1api.dll" (ByVal nPool As Long, ByVal hObject As Long, ByVal iPropertyList As Long, ByVal sName As Long) As Long
Declare Function TM1ProcessExecute Lib "tm1api.dll" (ByVal hPool As Long, ByVal hProcess As Long, ByVal hParametersArray As Long) As Long
Declare Function TM1ValType Lib "tm1api.dll" (ByVal hUser As Long, ByVal Value As Long) As Integer
Declare Sub TM1ValErrorString_VB Lib "tm1api.dll" (ByVal hUser As Long, ByVal vValue As Long, ByVal Str As String, ByVal Max As Integer)
Declare Function TM1ValBoolGet Lib "tm1api.dll" (ByVal hUser As Long, ByVal vBool As Long) As Integer
Declare Function TM1SystemServerDisconnect Lib "tm1api.dll" (ByVal hPool As Long, ByVal hDatabase As Long) As Long
Declare Sub TM1ValPoolDestroy Lib "tm1api.dll" (ByVal hPool As Long)
Declare Sub TM1SystemClose Lib "tm1api.dll" (ByVal hUser As Long)


Public Function p_ExecuteTM1process(ByVal tm1process As String, ByVal parameters As Variant, ByVal login As String, ByVal password As String) As Boolean
'---------------------------------------------------------------------------------------------
' Author:
'           Jim Wood
' Parameters:
'           tm1process  -> Name of TM1 process
'           parameters  -> Array containing the parameters for the TM1process
'           login       -> TM1 loginname
'           password    -> TM1 password
' Function:
'           Executes TM1 process 'tm1process'
'           REMARK:
'               Expects the following constants to be declared and initialized:
'                   Const c_ServerName   'Name TM1 server
'                   Const c_AdminHost    'Hostname of TM1 server
' Return:
'           TRUE     -> Function succeeded
'           FALSE    -> Function failed
'---------------------------------------------------------------------------------------------
On Error GoTo AfterError:
    Dim nServerName As Long
    Dim hServerName As Long
    Dim nClientID As Long
    Dim nPassword As Long
    Dim Retlong As Long
    Dim voprocess As Long
    Dim hParametersArray As Long
    Dim iparamarray() As Long
    Dim nNrOfParams As Long
    Dim sprocessname As String
    Dim RetType As Long
    Dim ErrorCode As Long
    Dim nI As Integer
    Dim sErrMsg As String * 100
    
    'Connection
    TM1APIInitialize
    hUser = TM1SystemOpen()
    If hUser = 0 Then
        MsgBox ("Error opening the system")
        GoTo ExitFunction
    End If
    
    'Create pool
    pGeneral = TM1ValPoolCreate(hUser)
    If pGeneral = 0 Then
        MsgBox ("Error creating memory pool")
        GoTo ExitFunction
    End If
    
    'Admin host
    Call TM1SystemAdminHostSet(hUser, c_AdminHost)
    Retlong = TM1SystemServerNof(hUser)
    
    ' Locate server and connect
    nServerName = TM1ValString(pGeneral, c_ServerName, 75)
    nClientID = TM1ValString(pGeneral, login, 75)
    nPassword = TM1ValString(pGeneral, password, 75)
    Call TM1SystemAdminHostSet(hUser, c_AdminHost)
    hServerName = TM1SystemServerConnect(pGeneral, nServerName, nClientID, nPassword)
    
    sprocessname = tm1process
    
    nNrOfParams = UBound(parameters) + 1
    ReDim iparamarray(nNrOfParams - 1)
    
    'For each parameter
    For nI = 1 To nNrOfParams
        iparamarray(nI - 1) = TM1ValString(pGeneral, parameters(nI - 1), Len(parameters(nI - 1)))
    Next nI
    
    hParametersArray = TM1ValArray(pGeneral, iparamarray, nNrOfParams)
    
    'For each parameter
    For nI = 1 To nNrOfParams
        TM1ValArraySet hParametersArray, iparamarray(nI - 1), nI
    Next nI
    
    voprocess = TM1ObjectListHandleByNameGet(pGeneral, hServerName, TM1ServerProcesses, TM1ValString(pGeneral, sprocessname, 100))
    
    Retlong = TM1ProcessExecute(pGeneral, voprocess, hParametersArray)
    RetType = TM1ValType(hUser, Retlong)
    
    If RetType = TM1ValTypeError Then 'TM1ValTypeObject Then
    'Return value is an error object
        TM1ValErrorString_VB hUser, Retlong, sErrMsg, 100
        Err.Raise vbObjectError + 514, , sErrMsg
    Else
    'Return value is a boolean value
        Retlong = TM1ValBoolGet(hUser, Retlong)
        If Retlong = 0 Then
            Err.Raise vbObjectError + 515, , "Process generated errors" & vbCrLf & "Check log file."
        Else
            'Process executed successfully
        End If
    End If

    p_ExecuteTM1process = True
ExitFunction:
    'DECONNECTION
    If pGeneral > 0 And hServerName > 0 Then
        Retlong = TM1SystemServerDisconnect(pGeneral, hServerName)
    End If
    
    'Destruction of pool
    If pGeneral > 0 Then
        TM1ValPoolDestroy (pGeneral)
    End If
    
    If hUser > 0 Then
        Call TM1SystemClose(hUser)
    End If
    Exit Function

AfterError:
    MsgBox "p_ExecuteTM1process: There was an error while executing process '" & tm1process & "':" & vbCr & vbCr & Err.Description, vbCritical, "Oops"
    Resume ExitFunction
End Function


Sub Button_proc_load()
    Call p_ExecuteTM1process("fwibble_proc", 0, "admin", "pear")
End Sub
Struggling through the quagmire of life to reach the other side of who knows where.
Shop at Amazon
Jimbo PC Builds on YouTube
OS: Mac OS 11 PA Version: 2.0.7
User avatar
Oratia623
Posts: 40
Joined: Mon Apr 27, 2009 5:36 am
OLAP Product: TM1/PA/CA
Version: V7.x to 2.0.9+
Excel Version: All
Location: Sydney, Australia

Re: VBA Process Run Issue Encountered

Post by Oratia623 »

*Disclaimer* I haven't any testing, but I believe the issue you are having is with the second variable you are passing to the p_ExecuteTM1process function from Button_proc_load.
The way the second variable is used in the function looks as if it is designed to be an array of parameters for the TI.
You have the following in Button_proc_load:
Call p_ExecuteTM1process("fwibble_proc", 0, "admin", "pear")
The second variable is not an array, I assume from this that the TI "fwibble_proc" has no parameters?

If you create an empty array and pass this through instead of 0, does it work?
eg:
Sub Button_proc_load()
Dim myparam(0) As Variant
Call p_ExecuteTM1process("pwtest", 0, "admin", "apple")
End Sub
Paul Williamson
____________________________________________________________________________________
I came. I saw. I did not concur.
User avatar
Martin Ryan
Site Admin
Posts: 1988
Joined: Sat May 10, 2008 9:08 am
OLAP Product: TM1
Version: 10.1
Excel Version: 2010
Location: Wellington, New Zealand
Contact:

Re: VBA Process Run Issue Encountered

Post by Martin Ryan »

If you've started with Mike Cowie's code (and it looks like you have) then the container for the parameters needs to be an array (specifically a ParamArray) and this array needs to be the last argument so the calling procedure can rattle off n parameters without getting confused by other parameters (c.f. http://forums.olapforums.com/viewtopic. ... 410#p15379).

Martin
Please do not send technical questions via private message or email. Post them in the forum where you'll probably get a faster reply, and everyone can benefit from the answers.
Jodi Ryan Family Lawyer
User avatar
jim wood
Site Admin
Posts: 3953
Joined: Wed May 14, 2008 1:51 pm
OLAP Product: TM1
Version: PA 2.0.7
Excel Version: Office 365
Location: 37 East 18th Street New York
Contact:

Re: VBA Process Run Issue Encountered

Post by jim wood »

I have changed the parameters around and it looks like it runs but the process does not updat ethe cube. Very strange. It just goes to hour glass for a second and then comes back.
Struggling through the quagmire of life to reach the other side of who knows where.
Shop at Amazon
Jimbo PC Builds on YouTube
OS: Mac OS 11 PA Version: 2.0.7
User avatar
jim wood
Site Admin
Posts: 3953
Joined: Wed May 14, 2008 1:51 pm
OLAP Product: TM1
Version: PA 2.0.7
Excel Version: Office 365
Location: 37 East 18th Street New York
Contact:

Re: VBA Process Run Issue Encountered

Post by jim wood »

My revised code:

Code: Select all

Public Const c_ServerName = "tm1development"
Public Const c_AdminHost = "sjmb089as"
Declare Sub TM1APIInitialize Lib "tm1api.dll" ()
Declare Function TM1SystemOpen Lib "tm1api.dll" () As Long
Declare Function TM1ValPoolCreate Lib "tm1api.dll" (ByVal hUser As Long) As Long
Declare Sub TM1SystemAdminHostSet Lib "tm1api.dll" (ByVal hUser As Long, ByVal AdminHosts As String)
Declare Function TM1SystemServerNof Lib "tm1api.dll" (ByVal hUser As Long) As Integer
Declare Function TM1ValString Lib "tm1api.dll" (ByVal hPool As Long, ByVal InitString As String, ByVal MaxSize As Long) As Long
Declare Function TM1SystemServerConnect Lib "tm1api.dll" (ByVal hPool As Long, ByVal sDatabase As Long, ByVal sClient As Long, ByVal sPassword As Long) As Long
Declare Function TM1ValArray Lib "tm1api.dll" (ByVal hPool As Long, ByRef sArray() As Long, ByVal MaxSize As Long) As Long
Declare Sub TM1ValArraySet Lib "tm1api.dll" (ByVal vArray As Long, ByVal val As Long, ByVal index As Long)
Declare Function TM1ObjectListHandleByNameGet Lib "tm1api.dll" (ByVal nPool As Long, ByVal hObject As Long, ByVal iPropertyList As Long, ByVal sName As Long) As Long
Declare Function TM1ProcessExecute Lib "tm1api.dll" (ByVal hPool As Long, ByVal hProcess As Long, ByVal hParametersArray As Long) As Long
Declare Function TM1ValType Lib "tm1api.dll" (ByVal hUser As Long, ByVal Value As Long) As Integer
Declare Sub TM1ValErrorString_VB Lib "tm1api.dll" (ByVal hUser As Long, ByVal vValue As Long, ByVal Str As String, ByVal Max As Integer)
Declare Function TM1ValBoolGet Lib "tm1api.dll" (ByVal hUser As Long, ByVal vBool As Long) As Integer
Declare Function TM1SystemServerDisconnect Lib "tm1api.dll" (ByVal hPool As Long, ByVal hDatabase As Long) As Long
Declare Sub TM1ValPoolDestroy Lib "tm1api.dll" (ByVal hPool As Long)
Declare Sub TM1SystemClose Lib "tm1api.dll" (ByVal hUser As Long)


Public Function p_ExecuteTM1process(ByVal tm1process As String, ByVal login As String, ByVal password As String, ParamArray parameters()) As String
'---------------------------------------------------------------------------------------------
' Author:
'           Jim Wood
' Parameters:
'           tm1process  -> Name of TM1 process
'           parameters  -> Array containing the parameters for the TM1process
'           login       -> TM1 loginname
'           password    -> TM1 password
' Function:
'           Executes TM1 process 'tm1process'
'           REMARK:
'               Expects the following constants to be declared and initialized:
'                   Const c_ServerName   'Name TM1 server
'                   Const c_AdminHost    'Hostname of TM1 server
' Return:
'           TRUE     -> Function succeeded
'           FALSE    -> Function failed
'---------------------------------------------------------------------------------------------
On Error GoTo AfterError:
    Dim nServerName As Long
    Dim hServerName As Long
    Dim nClientID As Long
    Dim nPassword As Long
    Dim Retlong As Long
    Dim voprocess As Long
    Dim hParametersArray As Long
    Dim iparamarray() As Long
    Dim nNrOfParams As Long
    Dim sprocessname As String
    Dim RetType As Long
    Dim ErrorCode As Long
    Dim nI As Integer
    Dim sErrMsg As String * 100
    
    'Connection
    TM1APIInitialize
    hUser = TM1SystemOpen()
    If hUser = 0 Then
        MsgBox ("Error opening the system")
        GoTo ExitFunction
    End If
    
    'Create pool
    pGeneral = TM1ValPoolCreate(hUser)
    If pGeneral = 0 Then
        MsgBox ("Error creating memory pool")
        GoTo ExitFunction
    End If
    
    'Admin host
    Call TM1SystemAdminHostSet(hUser, c_AdminHost)
    Retlong = TM1SystemServerNof(hUser)
    
    ' Locate server and connect
    nServerName = TM1ValString(pGeneral, c_ServerName, 75)
    nClientID = TM1ValString(pGeneral, login, 75)
    nPassword = TM1ValString(pGeneral, password, 75)
    Call TM1SystemAdminHostSet(hUser, c_AdminHost)
    hServerName = TM1SystemServerConnect(pGeneral, nServerName, nClientID, nPassword)
    
    sprocessname = tm1process
    
    nNrOfParams = UBound(parameters) + 1
    ReDim iparamarray(nNrOfParams - 1)
    
    'For each parameter
    For nI = 1 To nNrOfParams
        iparamarray(nI - 1) = TM1ValString(pGeneral, parameters(nI - 1), Len(parameters(nI - 1)))
    Next nI
    
    hParametersArray = TM1ValArray(pGeneral, iparamarray, nNrOfParams)
    
    'For each parameter
    For nI = 1 To nNrOfParams
        TM1ValArraySet hParametersArray, iparamarray(nI - 1), nI
    Next nI
    
    voprocess = TM1ObjectListHandleByNameGet(pGeneral, hServerName, TM1ServerProcesses, TM1ValString(pGeneral, sprocessname, 100))
    
    Retlong = TM1ProcessExecute(pGeneral, voprocess, hParametersArray)
    RetType = TM1ValType(hUser, Retlong)
    
    If RetType = TM1ValTypeError Then 'TM1ValTypeObject Then
    'Return value is an error object
        TM1ValErrorString_VB hUser, Retlong, sErrMsg, 100
        Err.Raise vbObjectError + 514, , sErrMsg
    Else
    'Return value is a boolean value
        Retlong = TM1ValBoolGet(hUser, Retlong)
        If Retlong = 0 Then
            Err.Raise vbObjectError + 515, , "Process generated errors" & vbCrLf & "Check log file."
        Else
            'Process executed successfully
        End If
    End If

    p_ExecuteTM1process = True
ExitFunction:
    'DECONNECTION
    If pGeneral > 0 And hServerName > 0 Then
        Retlong = TM1SystemServerDisconnect(pGeneral, hServerName)
    End If
    
    'Destruction of pool
    If pGeneral > 0 Then
        TM1ValPoolDestroy (pGeneral)
    End If
    
    If hUser > 0 Then
        Call TM1SystemClose(hUser)
    End If
    Exit Function

AfterError:
    MsgBox "p_ExecuteTM1process: There was an error while executing process '" & tm1process & "':" & vbCr & vbCr & Err.Description, vbCritical, "Oops"
    Resume ExitFunction
End Function


Sub Button_proc_load()
    Call p_ExecuteTM1process("fwibble_proc", "admin", "pear", "")
End Sub
Struggling through the quagmire of life to reach the other side of who knows where.
Shop at Amazon
Jimbo PC Builds on YouTube
OS: Mac OS 11 PA Version: 2.0.7
User avatar
jim wood
Site Admin
Posts: 3953
Joined: Wed May 14, 2008 1:51 pm
OLAP Product: TM1
Version: PA 2.0.7
Excel Version: Office 365
Location: 37 East 18th Street New York
Contact:

Re: VBA Process Run Issue Encountered

Post by jim wood »

Guys,

I think I have narroed it down. I think for some reason it is not able retrieve the process (object) name correctly. Have any of you encoutnered this problem before?

Martin I dropped your code in to my test sheet and it failed at the object check stage:

Code: Select all

ProcessHandle = TM1ObjectListHandleByNameGet(ValPoolHandle, ServerHandle, TM1ServerProcesses, TM1ValString(ValPoolHandle, ProcessName, 0))
  If TM1ValType(SessionHandle, ProcessHandle) <> TM1ValTypeObject Then
    RunTIProcess = "ERROR: Unable to access process " & ProcessName & "."
    GoTo Cleanup_API
  End If
I have run the debugger and both the SessionHandle and ProcessHandle are populated and I am logged in to my development server as admin,

Jim.
Struggling through the quagmire of life to reach the other side of who knows where.
Shop at Amazon
Jimbo PC Builds on YouTube
OS: Mac OS 11 PA Version: 2.0.7
Marcus Scherer
Community Contributor
Posts: 126
Joined: Sun Jun 29, 2008 9:33 am
OLAP Product: TM1
Version: 10.2.2
Excel Version: 2016
Location: Karlsruhe

Re: VBA Process Run Issue Encountered

Post by Marcus Scherer »

Hi Jim,

if you have no parameters in your process, set the following parameter to 0 (or modify determination of hParametersArray):

hParametersArray = TM1ValArray(pgeneral, iparamarray, 0)

This worked for me with your code.

HTH, Marcus
User avatar
Mike Cowie
Site Admin
Posts: 482
Joined: Sun May 11, 2008 7:07 pm
OLAP Product: IBM TM1/PA, SSAS, and more
Version: Anything thru 11.x
Excel Version: 2003 - Office 365
Location: Alabama, USA
Contact:

Re: VBA Process Run Issue Encountered

Post by Mike Cowie »

jim wood wrote:Guys,

I think I have narroed it down. I think for some reason it is not able retrieve the process (object) name correctly. Have any of you encoutnered this problem before?
Jim.
Hi Jim,

Sorry for the late reply to this thread - have you verified that the server handle is also valid (an object)? If for some reason the server connection failed you will obviously not be able to get a handle to the TI process. Sorry if that's something you're already checking, but I didn't spot that in your code.

Regards,
Mike
Mike Cowie
QueBIT Consulting, LLC

Are you lost without Print Reports in Planning Analytics for Excel (PAfE)? Get it back today, for free, with Print Reports for IBM Planning Analytics for Excel!
User avatar
jim wood
Site Admin
Posts: 3953
Joined: Wed May 14, 2008 1:51 pm
OLAP Product: TM1
Version: PA 2.0.7
Excel Version: Office 365
Location: 37 East 18th Street New York
Contact:

Re: VBA Process Run Issue Encountered

Post by jim wood »

Thanks for the suggestions guys. I'll have a look when I am able. The day job is getting in the way at the moment.
Struggling through the quagmire of life to reach the other side of who knows where.
Shop at Amazon
Jimbo PC Builds on YouTube
OS: Mac OS 11 PA Version: 2.0.7
User avatar
jim wood
Site Admin
Posts: 3953
Joined: Wed May 14, 2008 1:51 pm
OLAP Product: TM1
Version: PA 2.0.7
Excel Version: Office 365
Location: 37 East 18th Street New York
Contact:

Re: VBA Process Run Issue Encountered

Post by jim wood »

Marcus Scherer wrote:Hi Jim,

if you have no parameters in your process, set the following parameter to 0 (or modify determination of hParametersArray):

hParametersArray = TM1ValArray(pgeneral, iparamarray, 0)

This worked for me with your code.

HTH, Marcus
I have updated the code as suggested above and I am still not getting the same result as before. It look like it runs but the cube is not updated.

I'm going to try to run the process from another biox to see if it something to do with my dev server. (Which wouldn't be a suprise.)
Struggling through the quagmire of life to reach the other side of who knows where.
Shop at Amazon
Jimbo PC Builds on YouTube
OS: Mac OS 11 PA Version: 2.0.7
Marcus Scherer
Community Contributor
Posts: 126
Joined: Sun Jun 29, 2008 9:33 am
OLAP Product: TM1
Version: 10.2.2
Excel Version: 2016
Location: Karlsruhe

Re: VBA Process Run Issue Encountered

Post by Marcus Scherer »

see attached the complete code, in case I oversaw another change. The API declarations I usually store in a separate module.

Marcus
Attachments
API_declarations.txt
(35.91 KiB) Downloaded 251 times
proc_load.txt
(4.33 KiB) Downloaded 230 times
User avatar
jim wood
Site Admin
Posts: 3953
Joined: Wed May 14, 2008 1:51 pm
OLAP Product: TM1
Version: PA 2.0.7
Excel Version: Office 365
Location: 37 East 18th Street New York
Contact:

Re: VBA Process Run Issue Encountered

Post by jim wood »

Many thanks Marcus,

Jim.
Struggling through the quagmire of life to reach the other side of who knows where.
Shop at Amazon
Jimbo PC Builds on YouTube
OS: Mac OS 11 PA Version: 2.0.7
Post Reply