Pages

Saturday, 29 August 2015

How to call SAP function using Excel Macro

Introduction
In this blog we will see how we can call a remote execution enabled function. We can find out if a function is remote execution enabled by reading the attributes of the function source in se37. Each function has input and result or output. The input or output can be have format as simple string, number or table.


Example 1:
Function RFC_GET_TABLE_ENTRIES, has two input parameters Table_name and Max_entries. We need the the table name only but initializing other parameter ensure no junk values. So we export the input parameters and import the output/result. Below is a sample how we call this function...

 Sub Z_call_function()
'Logon
      Dim R3 As Object 
'Declare variant
      Set R3 = CreateObject("SAP.Functions") 
'Create ActiveX object
  'R3.Connection.SystemID = "XYz"
      R3.Connection.ApplicationServer = "Hostname"
      R3.Connection.User = "username"
      R3.Connection.Password = "password"
      R3.Connection.client = "100"
      R3.Connection.Language = "EN"
      R3.Connection.SystemNumber = "00"
      R3.Connection.CodePage = "1100"
'Establish connection using above details to SAP server
      If R3.Connection.Logon(0, True) <> True Then
             MsgBox "Cannot Log on to SAP" 'Issue message if cannot logon
      Else
             MsgBox "Logged on to SAP!"
      End If
'Define function
      Dim R3Func As Object
      Set R3Func = R3.Add("RFC_GET_TABLE_ENTRIES")
'Define and set values for input
      Dim TABLE_NAME, MAX_ENTRIES As Object
      Set TABLE_NAME = R3Func.exports("TABLE_NAME")
      TABLE_NAME.Value = "KNA1"
      Set MAX_ENTRIES = R3Func.exports("MAX_ENTRIES")
      MAX_ENTRIES.Value = "0"

      If R3Func.Call = False Then
            MsgBox R3Func.Exception
      End If
      NUMBER_OF_ENTRIES = R3Func.imports("NUMBER_OF_ENTRIES")
      MsgBox ("Table Enteries in KAN1 : " & NUMBER_OF_ENTRIES)

End Sub

Example 2: Function RFC_READ_Table
This particular function is used for many reporting purposes. Here we are going to use tables as input and output. We have an excel worksheet with name "Main". We have list of transports from Cell B10. as in image below


 Sub GetE07t(R3 As Object)
Zlogin R3
ZSID = "XYZ"
'Define function
      Dim myFunc1 As Object
      Set myFunc1 = R3.Add("RFC_READ_TABLE")

      Dim QUERY_TABLE1, RowCount1 As Object
      Set QUERY_TABLE1 = myFunc1.Exports("QUERY_TABLE")
      QUERY_TABLE1.Value = "E07T"
      ' We will not set RowCount input parameter as not needed
      'Set RowCount = myFunc1.Exports("ROWCOUNT")
      'RowCount.Value = "90"


' input is in form of tables so we need to add values to them
' In many cases these are used to set filter or special selection 
      Dim Options1, Fields1, Data1 As Object
      Set Options1 = myFunc1.Tables("OPTIONS")
      Set Fields1 = myFunc1.Tables("FIELDS")
      Set Data1 = myFunc1.Tables("DATA")


'First we set the condition
'Refresh table
      Options1.FreeTable
      Sheets("Main").Cells(2, 1) = "=counta(B10:B1000)"    ' 
      tr_count = Sheets("Main").Cells(2, 1) + 9
    
      For i = 10 To tr_count - 1
         'set values for tranport filter 
          Options1.Rows.Add
          Options1(Options1.RowCount, "TEXT") = "TRKORR like '" & Sheets("Main").Cells(i, 2) & "' OR"
      Next
          Options1.Rows.Add
          Options1(Options1.RowCount, "TEXT") = "TRKORR like '" & Sheets("Main").Cells(tr_count, 2) & "'"


'Refresh table
          Fields1.FreeTable
'Set values for what table fields output is needed
      Fields1.Rows.Add
      Fields1(Fields1.RowCount, "FIELDNAME") = "TRKORR"
      Fields1.Rows.Add
      Fields1(Fields1.RowCount, "FIELDNAME") = "AS4TEXT"


' call function
      If myFunc1.Call = False Then
          MsgBox myFunc1.Exception
      End If
    
' Write the output in excel sheet
      Dim objDatRec As Object
      Dim objFldRec As Object
      For Each objDatRec In Data1.Rows
          For Each objFldRec In Fields1.Rows
                 sheets("sheet2").Cells(objDatRec.Index, objFldRec.Index) = Mid(objDatRec("WA"), objFldRec("OFFSET") + 1, objFldRec("LENGTH"))
          Next
      Next
End Sub

the output is written in sheet2 of the same workbook
In my next blog, I will show how to perform monitoring using these concepts


1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete