I'm trying to automate certain procedures in SAP using Excel Templates and VBA.
To extract data, I'm using INST_EXECUTE_REPORT via RFC. I packed it into a separate Sub in VBA, in which I declare a local variable ObjR3_EXECUTE_REPORT:
Set ObjR3_EXECUTE_REPORT = ObjR3.Add("INST_EXECUTE_REPORT")
'Define SAP Tables
With ObjR3_EXECUTE_REPORT
Set ObjR3_EXECUTE_REPORT_Name = .Exports("PROGRAM")
Set ObjR3_EXECUTE_REPORT_Para = .Tables("PARA")
Set ObjR3_EXECUTE_REPORT_Result = .Tables("RESULT_TAB")
Set ObjR3_EXECUTE_REPORT_Output = .Tables("OUTPUT_TAB")
End With
'Define SAP source table
ObjR3_EXECUTE_REPORT_Name.Value = ReportName
FieldTablesArray = Array(ObjR3_EXECUTE_REPORT_Para, ObjR3_EXECUTE_REPORT_Result, ObjR3_EXECUTE_REPORT_Output) ',
For a = 0 To UBound(FieldTablesArray, 1)
Set InnerSAPTable = FieldTablesArray(a)
r = InnerSAPTable.RowCount
For f = 1 To r
InnerSAPTable.DeleteRow (1)
Next f
Next a
Dim aParameterPair() As Variant
Dim aParameterInput() As Variant
Dim sParameterName As String
Dim sParameterInput As String
'Build up the table with the fields to be sellected
f = 1
For a = LBound(aParameters) To UBound(aParameters)
aParameterPair = aParameters(a)
aParameterInput = aParameterPair(UBound(aParameterPair))
sParameterName = aParameterPair(LBound(aParameterPair))
For c = LBound(aParameterInput) To UBound(aParameterInput)
sParameterInput = aParameterInput(c)
ObjR3_EXECUTE_REPORT_Para.AppendRow
ObjR3_EXECUTE_REPORT_Para(f, "PARA_NAME") = sParameterName
ObjR3_EXECUTE_REPORT_Para(f, "PARA_VALUE") = sParameterInput
Debug.Print sParameterName & " " & sParameterInput
f = f + 1
Next c
Next a
CallResult = False
ObjR3_EXECUTE_REPORT.Imports("RESULT_TEXT").Clear
ObjR3_EXECUTE_REPORT_Output(1, 1) = sCaptionRow
'Call the ABAP function to extract the Report's output table to an object
CallResult = ObjR3_EXECUTE_REPORT.Call
'Debug.Print ReportName & ": " & ObjR3_EXECUTE_REPORT.Imports("RESULT_TEXT")
If CallResult = False Then
Debug.Print ObjR3_EXECUTE_REPORT.Imports("RESULT_TEXT")
Exit Sub
Else
Debug.Print ReportName & ": " & ObjR3_EXECUTE_REPORT_Output.RowCount & " data rows downloaded successfully)"
If ObjR3_EXECUTE_REPORT_Output.RowCount = 0 Then Exit Sub
End If
Please note that ObjR3 is a public variable:
Public ObjR3 As Object
Set ObjR3 = CreateObject("SAP.Functions")
To achieve the result I want, I first need to get the GL Account balances using program ReportName = "RFSSLD00" then, if the balance of a specific account is not 0, I want to download all individual transactions, but only in the last 60 days, using program ReportName = "RFITEMGL".
As long as there is at least one transaction in the last 60 days, the whole process works as expected. However, if there is no transaction in this time period, instead of returning a blank result, SAP returns the result of the ReportName = "RFSSLD00" program as soon as the ObjR3_EXECUTE_REPORT.Call function is executed.
I executed the routine in break mode, and before the second time ObjR3_EXECUTE_REPORT.Call is executed, the ObjR3_EXECUTE_REPORT_Output.RowCount of the Output table is 0. As soon as the call is ecexuted, so it was empty before the call.
If I reverse the order of both steps, i.e. first check for transactions in the last 60 days (RFITEMGL), then get the balance (RFSSLD00), the ObjR3_EXECUTE_REPORT_Output.RowCount for RFITEMGL is 0, as expected. However, I need the correct order for the procedure to make sense.
I'm at a loss here. Is there any way to make SAP "forget" the result of the first ObjR3_EXECUTE_REPORT.Call ? I tried removing the ObjR3_EXECUTE_REPORT, but it didn't work:
ObjR3.Remove ObjR3_EXECUTE_REPORT
Any suggestions would be welcome.
Best regards,