BAPI-VB, Unable to call method GetDetail of USER Object using SAP.BAPI.1

Advertisement
Hi,
I am trying to call SAP Methods using Excel VBA.
In the below example, I am trying to get the user details.
I can solve this, if I use SAP.Functions object, but not when I use SAP.BAPI.1 object.
The Procedure GetUserDetails() works fine, but the 2nd one GetUserDetails2() fails?
Can you tell what is the difference in calling SAP method with SAP.Functions and SAP.BAPI.1 ?
Also how can I run the program GetDetails2() using SAP.BAPI.1
Const CNT_STR_USR As String = "XXXXX"
Const CNT_STR_PWD As String = "XXXXX"
Const CNT_STR_APPLN_SRVR As String = "ides47"
Const CNT_STR_SYSTEM As String = "IDS"
Const CNT_STR_SYS_NUM As String = "00"
Const CNT_STR_CLIENT As String = "800"
Const CNT_STR_LOGON_LANG As String = "EN"
Const CNT_STR_LOG_FILE As String = "C:sap_vb.txt"
Const CNT_INT_LOG_LEVEL As Integer = 9
'Works Fine
Public Sub GetUserDetails()
'Using SAP Functions
Dim obSAPFn As Object
Dim obFuncUsrDtl As Object
Dim obFuncRtrn As Object
Dim obFuncLogDtl As Object
Dim sRetStatus As String * 1, sErrText As String, sUsrGroup As String * 12
Dim iRetRowCount As Integer, iLoop As Integer
Dim bErrFlag As Boolean
'Set obSAPFn = New SAPFunctions
Set obSAPFn = CreateObject("SAP.Functions")
obSAPFn.Connection.ApplicationServer = CNT_STR_APPLN_SRVR
obSAPFn.Connection.SystemNumber = CNT_STR_SYS_NUM
obSAPFn.Connection.User = CNT_STR_USR
obSAPFn.Connection.Password = CNT_STR_PWD
obSAPFn.Connection.Language = CNT_STR_LOGON_LANG
obSAPFn.Connection.Client = CNT_STR_CLIENT
obSAPFn.LogLevel = CNT_INT_LOG_LEVEL
obSAPFn.LogFileName = CNT_STR_LOG_FILE
'Check For Connection
If obSAPFn.Connection.Logon(0, True) = False Then
    MsgBox "R/3 connection failed"
Exit Sub
Else
    If obSAPFn.Connection.IsConnected Then
'    MsgBox "Connected"
    Else
    MsgBox "Not COnnected"
    Exit Sub
    End If
End If
'Get User Details.
Set obFuncUsrDtl = obSAPFn.Add("BAPI_USER_GET_DETAIL")
obFuncUsrDtl.Exports("USERNAME") = CNT_STR_USR
obFuncUsrDtl.Call
Set obFuncRtrn = obFuncUsrDtl.Tables("RETURN")
iRetRowCount = obFuncRtrn.RowCount
bErrFlag = False
For iLoop = 1 To iRetRowCount
    If obFuncRtrn(iLoop, "TYPE") = "E" Then
'        ErrorUsuario = True
        sErrText = "E" & obFuncRtrn(iLoop, "ID") & obFuncRtrn(iLoop, "NUMBER") & _
                     " " & obFuncRtrn(iLoop, "MESSAGE")
        MsgBox sErrText
        bErrFlag = True
        Exit For
    End If
Next
If bErrFlag = False Then
Set obFuncLogDtl = obFuncUsrDtl.Imports("LOGONDATA")
sUsrGroup = obFuncLogDtl("CLASS")
MsgBox sUsrGroup
End If
Set obFuncRtrn = Nothing
Set obFuncLogDtl = Nothing
Set obFuncUsrDtl = Nothing
obSAPFn.Connection.LogOff
Set obSAPFn = Nothing
End Sub
' Does not work
Public Sub GetUserDetails2()
'Using BAPI Object
Dim obSapBAPICtrl As Object 'BAPI control object
'Dim obSAPConn As Object 'Connection object
Dim obSAPUSER As Object ' To Get Details of USER Object
Dim obLogondata As Object, obDefaults As Object, obAddress As Object, obCompany As Object
Dim obSnc As Object, obParameter As Object, obProfiles As Object, obActivitygroups As Object
Dim obReturn As Object, obAddComrem As Object, obAddRml As Object, obAddPag As Object
Dim obAddUri As Object, obAddSsf As Object, obAddPrt As Object, obAddRfc As Object
Dim obAddX400 As Object, obAddSmtp As Object, obAddTlx As Object, obAddTtx As Object
Dim obAddTel As Object, obAddFax As Object, obParameter1 As Object
Dim sRetStatus As String * 1, sTransId As String, sUsrGroup As String
Dim iRetRowCount As Integer, iLoop As Integer
Set obSapBAPICtrl = CreateObject("SAP.BAPI.1")
obSapBAPICtrl.Connection.ApplicationServer = CNT_STR_APPLN_SRVR
obSapBAPICtrl.Connection.SystemNumber = CNT_STR_SYS_NUM
obSapBAPICtrl.Connection.User = CNT_STR_USR
obSapBAPICtrl.Connection.Password = CNT_STR_PWD
obSapBAPICtrl.Connection.Language = CNT_STR_LOGON_LANG
obSapBAPICtrl.Connection.Client = CNT_STR_CLIENT
obSapBAPICtrl.LogLevel = CNT_INT_LOG_LEVEL
obSapBAPICtrl.LogFileName = CNT_STR_LOG_FILE
'Don't show the logon details
'Connect to SAP
If obSapBAPICtrl.Connection.Logon(0, True) = False Then
    MsgBox "R/3 connection failed"
    Exit Sub
Else
    If obSapBAPICtrl.Connection.IsConnected Then
'    MsgBox "Connected"
    Else
    MsgBox "Not COnnected"
    Exit Sub
    End If
End If
‘Could not find a way to pass the User Id?
‘ Is this the right way to pass the user Id for this Object?
Set obSAPUSER = obSapBAPICtrl.GetSAPObject("USER", CNT_STR_USR)
Set obLogondata = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "Logondata")
Set obDefaults = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "Defaults")
Set obAddress = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "Address")
Set obCompany = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "Company")
Set obSnc = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "Snc")
Set obParameter = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "Parameter")
Set obProfiles = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "Profiles")
Set obActivitygroups = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "Activitygroups")
Set obReturn = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "Return")
Set obAddComrem = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "AddComrem")
Set obAddRml = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "AddRml")
Set obAddPag = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "AddPag")
Set obAddUri = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "AddUri")
Set obAddSsf = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "AddSsf")
Set obAddPrt = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "AddPrt")
Set obAddRfc = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "AddRfc")
Set obAddX400 = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "AddX400")
Set obAddSmtp = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "AddSmtp")
Set obAddTlx = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "AddTlx")
Set obAddTtx = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "AddTtx")
Set obAddTel = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "AddTel")
Set obAddFax = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "AddFax")
Set obParameter1 = obSapBAPICtrl.DimAs(obSAPUSER, "GetDetail", "Parameter1")
sTransId = obSapBAPICtrl.CreateTransactionID()
'obSapBAPICtrl.TransactionId = sTransId
obSAPUSER.GetDetail Logondata:=obLogondata, Defaults:=obDefaults, Address:=obAddress, _
Company:=obCompany, Snc:=obSnc, Parameter:=obParameter, Profiles:=obProfiles, _
Activitygroups:=obActivitygroups, Return:=obReturn, AddComrem:=obAddComrem, _
AddRml:=obAddRml, AddPag:=obAddPag, AddUri:=obAddUri, AddSsf:=obAddSsf, _
AddPrt:=obAddPrt, AddRfc:=obAddRfc, AddX400:=obAddX400, AddSmtp:=obAddSmtp, _
AddTlx:=obAddTlx, AddTtx:=obAddTtx, AddTel:=obAddTel, AddFax:=obAddFax, _
Parameter1:=obParameter
iRetRowCount = obReturn.RowCount
If iRetRowCount > 0 Then
    For iLoop = 0 To iRetRowCount
        sRetStatus = obReturn(iLoop, "TYPE")
        If sRetStatus = "S" Then
        sUsrGroup = obLogondata("CLASS")
        MsgBox sUsrGroup
        End If
    Next
End If
Set obSAPUSER = Nothing
obSapBAPICtrl.Connection.LogOff
'Set obSAPConn = Nothing
Set obSapBAPICtrl = Nothing
End Sub
Regards,
Vikas
Advertisement

Replay

The problem was occuring because, the structure Return was not holding any values.
But the other tables which returned values were getting populated.
Regards,
Vikas