Skip to Content

Hello community,

I presented COM Connector CCo for SAP NetWeaver RFC library here.

With CCo it is very easy possible to execute remote-enabled function modules.

Here now an example how to execute the FM RFC_ABAP_INSTALL_AND_RUN with the scripting languages VBScript and AutoIt Script. This FM offers the possibility to executes ABAP code from a program which comes from outside of the SAP environment.

The first example in VBScript:

'-Begin-----------------------------------------------------------------

  '-Directives----------------------------------------------------------
    Option Explicit

  '-Constants-----------------------------------------------------------
    Const RFC_OK = 0

  '-Sub Main------------------------------------------------------------
    Sub Main()

      '-Variables-------------------------------------------------------
        Dim SAP, hRFC, rc, hFuncDesc, hFunc, hTable, i, hRow
        Dim RowCount, charBuffer, Result

      '-ABAPReport------------------------------------------------------
      '-
      '- Code your ABAP report here. The length of each line must be
      '- equal or less than 72 characters.
      '-
      '-----------------------------------------------------------------
        Dim ABAP(3)
        ABAP(0) = "Report zTest Line-Size 256."
        ABAP(1) = "Write: 'Hello World from'."
        ABAP(2) = "Write: sy-sysid."

      Set SAP = CreateObject("COMNWRFC")
      If Not IsObject(SAP) Then
        Exit Sub
      End If

      hRFC = SAP.RfcOpenConnection("ABAP", "00", "001", "BCUSER")
      If hRFC = 0 Then
        Set SAP = Nothing
        Exit Sub
      End If

      hFuncDesc = SAP.RfcGetFunctionDesc(hRFC, _
        "RFC_ABAP_INSTALL_AND_RUN")
      If hFuncDesc = 0 Then
        rc = SAP.RfcCloseConnection(hRFC)
        Set SAP = Nothing
        Exit Sub
      End If

      hFunc = SAP.RfcCreateFunction(hFuncDesc)
      If hFunc = 0 Then
        rc = SAP.RfcCloseConnection(hRFC)
        Set SAP = Nothing
        Exit Sub
      End If

      '-Writes the report into the PROGRAM table------------------------
        If SAP.RfcGetTable(hFunc, "PROGRAM", hTable) = RFC_OK Then
          For i = 0 To UBound(ABAP) - 1
            hRow = SAP.RfcAppendNewRow(hTable)
            rc = SAP.RfcSetChars(hRow, "LINE", ABAP(i))
          Next
        End If

      If SAP.RfcInvoke(hRFC, hFunc) = RFC_OK Then
        '-Gets the result from the WRITES table-------------------------
          If SAP.RfcGetTable(hFunc, "WRITES", hTable) = RFC_OK Then
            rc = SAP.RfcGetRowCount(hTable, RowCount)
            rc = SAP.RfcMoveToFirstRow(hTable)
            For i = 1 To RowCount
              hRow = SAP.RfcGetCurrentRow(hTable)
              rc = SAP.RfcGetChars(hRow, "ZEILE", charBuffer, 256)
              Result = Result & Trim(charBuffer) & vbCrLf
              If i < RowCount Then
                rc = SAP.RfcMoveToNextRow(hTable)
              End If
            Next
            '-Shows the result in a messagebox--------------------------
              MsgBox Result, vbOkOnly, "Result"
          End If
      End If

      rc = SAP.RfcDestroyFunction(hFunc)
      rc = SAP.RfcCloseConnection(hRFC)
      Set SAP = Nothing

    End Sub

  '-Main----------------------------------------------------------------
    Main

'-End-------------------------------------------------------------------

The second example in AutoIt Script:

;-Begin-----------------------------------------------------------------

  ;-Directives----------------------------------------------------------
    AutoItSetOption("MustDeclareVars", 1)

  ;-Constants-----------------------------------------------------------
    Const $RFC_OK = 0

  ;-Variables-----------------------------------------------------------
    Dim $SAP, $hRFC, $hFuncDesc, $hFunc, $hTable = 0, $RowCount = 0
    Dim $i, $hRow, $charBuffer = "", $Result

  ;-ABAPReport----------------------------------------------------------
  ;-
  ;- Code your ABAP report here. The length of each line must be
  ;- equal or less than 72 characters.
  ;-
  ;---------------------------------------------------------------------
    Dim $ABAP[3]
    $ABAP[0] = "Report zTest Line-Size 256."
    $ABAP[1] = "Write: 'Hello World from'."
    $ABAP[2] = "Write: sy-sysid."

  ;-Main----------------------------------------------------------------
    $SAP = ObjCreate("COMNWRFC")
    If Not IsObj($SAP) Then
      Exit
    EndIf

    $hRFC = $SAP.RfcOpenConnection("ABAP", "00", "001", "BCUSER")
    If Not $hRFC Then
      $SAP = 0
      Exit
    EndIf

    $hFuncDesc = $SAP.RfcGetFunctionDesc($hRFC, _
      "RFC_ABAP_INSTALL_AND_RUN")
    If Not $hFuncDesc Then
      $SAP.RfcCloseConnection($hRFC)
      $SAP = 0
      Exit
    EndIf

    $hFunc = $SAP.RfcCreateFunction($hFuncDesc)
    If Not $hFunc Then
      $SAP.RfcCloseConnection($hRFC)
      $SAP = 0
      Exit
    EndIf

    ;-Writes the report into the PROGRAM table--------------------------
      If $SAP.RfcGetTable($hFunc, "PROGRAM", $hTable) = $RFC_OK Then
        For $i = 0 To UBound($ABAP) - 1
          $hRow = $SAP.RfcAppendNewRow($hTable)
          $SAP.RfcSetChars($hRow, "LINE", $ABAP[$i])
        Next
      EndIf

    If $SAP.RfcInvoke($hRFC, $hFunc) = $RFC_OK Then
      ;-Gets the result from the WRITES table---------------------------
        If $SAP.RfcGetTable($hFunc, "WRITES", $hTable) = $RFC_OK Then
          $SAP.RfcGetRowCount($hTable, $RowCount)
          $SAP.RfcMoveToFirstRow($hTable)
          For $i = 1 To $RowCount
            $hRow = $SAP.RfcGetCurrentRow($hTable)
            $SAP.RfcGetChars($hRow, "ZEILE", $charBuffer, 256)
            $Result = $Result & StringStripWS($charBuffer, 3) & @CrLf
            If $i < $RowCount Then
              $SAP.RfcMoveToNextRow($hTable)
            EndIf
          Next
          ;-Shows the result in a messagebox----------------------------
            MsgBox(0, "Result", $Result)
        EndIf
    EndIf

    $ABAP = 0
    $SAP.RfcDestroyFunction($hFunc)
    $SAP.RfcCloseConnection($hRFC)
    $SAP = 0

;-End-------------------------------------------------------------------

 

In both examples we get this result:

0108_ABAPReport_Result.JPG

 

You see how easy it is to contain an ABAP report inside a script.

 

Cheers
Stefan

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply