Skip to Content
Technical Articles
Author's profile photo Stefan Schnell

How To Get Remote-Enable Function Modules Documentation in a Word Document with VBA and CCo

Here an example how to get the documentation from remote-enabled function modules and to write it in a Word document. The solution is coded in VBA in Microsoft Word application and uses the COM Connector (CCo). It is possible to use a pattern for the function modules name (FMPattern), on this way you can create a documentation for a group of function modules, e.g. a package, easily. If you disable the FMODE selection criterion you can use this solution for any function module.

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

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

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

  '-Sub GetFMDocu-------------------------------------------------------
  '-
  '- This sub procedure detects in a first step all names from
  '- remote-enabled function modules with a specific name pattern.
  '- In a second step it gets the documentation of the function module
  '- and writes this information to the Word document.
  '-
  '---------------------------------------------------------------------
    Sub GetFMDocu()

      '-Variables-------------------------------------------------------
        #If Win64 Then
          Dim SAP As Object
        #Else
          Dim SAP As CCo.COMNWRFC
        #End If
        Dim hRFC As Long
        Dim rc As Integer
        Dim hFunc As Long
        Dim hFuncDesc As Long
        Dim hTable As Long
        Dim hRow As Long
        Dim rowCount As Long
        Dim FuncName As String
        Dim i As Long
        Dim j As Long
        Dim FuncNames() As String
        Dim Parameter As String
        Dim Line As String
        
        Dim FMPattern As String
        FMPattern = "RFC%"

      Set SAP = CreateObject("COMNWRFC")
      If Not IsObject(SAP) Then
        Exit Sub
      End If
      
      hRFC = SAP.RfcOpenConnection("ASHOST=NSP, SYSNR=00, " & _
        "CLIENT=001, USER=BCUSER")
      If hRFC = 0 Then
        Set SAP = Nothing
        Exit Sub
      End If
      
      '-Detect remote-enabled function modules--------------------------
      hFuncDesc = SAP.RfcGetFunctionDesc(hRFC, "RFC_READ_TABLE")
      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
              
      rc = SAP.RfcSetChars(hFunc, "QUERY_TABLE", "TFDIR")
      rc = SAP.RfcSetChars(hFunc, "DELIMITER", "~")
      If SAP.RfcGetTable(hFunc, "OPTIONS", hTable) = RFC_OK Then
        hRow = SAP.RfcAppendNewRow(hTable)
        rc = SAP.RfcSetChars(hRow, "TEXT", "FUNCNAME LIKE '" & _
          FMPattern & "' AND FMODE = 'R'")
      End If
      If SAP.RfcGetTable(hFunc, "FIELDS", hTable) = RFC_OK Then
        hRow = SAP.RfcAppendNewRow(hTable)
        rc = SAP.RfcSetChars(hRow, "FIELDNAME", "FUNCNAME")
      End If
      
      If SAP.RfcInvoke(hRFC, hFunc) = RFC_OK Then
        rc = SAP.RfcGetTable(hFunc, "DATA", hTable)
        If SAP.RfcGetRowCount(hTable, rowCount) = RFC_OK Then
          rc = SAP.RfcMoveToFirstRow(hTable)
          For i = 1 To rowCount
            hRow = SAP.RfcGetCurrentRow(hTable)
            rc = SAP.RfcGetChars(hRow, "WA", FuncName, 512)
            ReDim Preserve FuncNames(i)
            FuncNames(i) = Trim(FuncName)
            If i < rowCount Then
              rc = SAP.RfcMoveToNextRow(hTable)
            End If
          Next
        End If
      End If
              
      rc = SAP.RfcDestroyFunction(hFunc)

      '-Get function module documentation-------------------------------
      hFuncDesc = SAP.RfcGetFunctionDesc(hRFC, "RFC_FUNCTION_DOCU_GET")
      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

      For i = 1 To UBound(FuncNames())
        Selection.TypeText FuncNames(i)
        Selection.InsertBreak wdLineBreak
        
        rc = SAP.RfcSetChars(hFunc, "FUNCNAME", FuncNames(i))
        rc = SAP.RfcSetChars(hFunc, "LANGUAGE", "D")
        If SAP.RfcInvoke(hRFC, hFunc) = RFC_OK Then
          rc = SAP.RfcGetTable(hFunc, "FUNCDOCU", hTable)
          If SAP.RfcGetRowCount(hTable, rowCount) = RFC_OK Then
            If rowCount > 0 Then
              rc = SAP.RfcMoveToFirstRow(hTable)
              For j = 1 To rowCount
                hRow = SAP.RfcGetCurrentRow(hTable)
                rc = SAP.RfcGetChars(hRow, "PARAMETER", Parameter, 30)
                Parameter = Trim(Parameter)
                If Parameter <> "" Then
                  Selection.TypeText Parameter & " - "
                End If
                rc = SAP.RfcGetChars(hRow, "TDLINE", Line, 132)
                Selection.TypeText Trim(Line)
                Selection.InsertBreak wdLineBreak
                If j < rowCount Then
                  rc = SAP.RfcMoveToNextRow(hTable)
                End If
              Next
            End If
          End If
        
        End If
        
        Selection.InsertBreak wdPageBreak
      Next

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

    End Sub

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

Here an example from RFC_READ_TABLE in Word.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.