<!-- Begin--------------------------------------------------------------

  This HTML and VBScript source shows how to connect a SAP system via
  SAP ActiveX control libraries, to read data from a SAP table and to
  view this data in a HTML table, with header line.

  Author:  Stefan Schnell
  Version: 0.81

-------------------------------------------------------------------- -->

<html>

  <head>

    <title>
      Connect a SAP system and view table content
    </title>

    <hta:application applicationname="ReadSAPTable" id="ReadSAPTable"
      version="1.0"/>

    <!-- To encode your source, use the Script Encoder screnc.exe -->
    <script language="VBScript.Encode">

      Option Explicit

      '-Function Connect------------------------------------------------
      '-
      '- Connects a SAP system
      '-
      '-----------------------------------------------------------------
      Function Connect(ByVal SAPFunc, ByRef Connection)

        '-Get SAP.LogonControl connection-------------------------------
        Set Connection = SAPFunc.Connection()
        If Not IsObject(Connection) Then
          MsgBox "No SAP.Connection instance", vbOKOnly, "Error"
          Connect = vbFalse
          Exit Function
        End If

        '-Set connection parameters-------------------------------------
        Connection.Client = CStr(Document.eingabe.Client.Value)
        Connection.User = CStr(Document.eingabe.User.Value)
        Connection.Password = CStr(Document.eingabe.Password.Value)
        Connection.Language = "EN"
        Connection.System = CStr(Document.eingabe.System.Value)
        Connection.HostName = CStr(Document.eingabe.HostName.Value)
        Connection.SystemNumber = _
          CStr(Document.eingabe.SystemNumber.Value)

        '-Connect SAP system--------------------------------------------
        Connect = Connection.Logon(0, vbTrue)

      End Function

      '-Sub GetData-----------------------------------------------------
      '-
      '- Gets the content of a SAP table
      '-
      '-----------------------------------------------------------------
      Sub GetData()

        Dim SAPFunc, Connection, ReadTableFunc, Param, Table, Fields
        Dim RowCount, i, j, DataLine, Node, HTMLTable, SAPTableName
        Dim tr, td, arrDataLine 

        '-Get the name of the table-------------------------------------
        SAPTableName = CStr(Document.eingabe.SAPTableName.Value)
        If Trim(SAPTableName) = "" Then
          MsgBox "No SAP table name specify", vbOKOnly, "Error"
          Exit Sub
        End If

        '-Get SAP.Functions---------------------------------------------
        '-
        '- If it is not possible to create SAP.Functions instance,
        '- Microsoft(c) Internet Explorer(c) creates an error
        '-
        '---------------------------------------------------------------
        Set SAPFunc = CreateObject("SAP.Functions")
        If Not IsObject(SAPFunc) Then
          Exit Sub
        End If

        '-Set log level and file----------------------------------------
        SAPFunc.LogLevel = 7
        SAPFunc.LogFileName = "C:\Dummy\LogHTML.txt"

        If Connect(SAPFunc, Connection) = vbTrue Then

          '-Get function module RFC_READ_TABLE--------------------------
          Set ReadTableFunc = SAPFunc.Add("RFC_READ_TABLE")

          If IsObject(ReadTableFunc) Then

            '-Clear HTML table------------------------------------------
            Set HTMLTable = Document.getElementById("Tabelle")
            j = HTMLTable.childNodes.length
            If j > 0 Then
              For i = 1 To j
                Set Node = HTMLTable.lastChild
                HTMLTable.removeChild(Node)
              Next
              Set Node = Nothing
            End If

            '-Define export parameter DELIMITER to ~--------------------
            Set Param = ReadTableFunc.Exports("DELIMITER")
            Param.Value = "~"

            '-Get SAP table data----------------------------------------
              '-Define export parameter QUERY_TABLE---------------------
              Set Param = ReadTableFunc.Exports("QUERY_TABLE")
              Param.Value = SAPTableName
              '-Delete entries------------------------------------------
              ReadTableFunc.Tables("OPTIONS").Rows.RemoveAll
              ReadTableFunc.Tables("FIELDS").Rows.RemoveAll
              '-Read table----------------------------------------------
              If ReadTableFunc.Call() = vbTrue Then
                Set Table = ReadTableFunc.Tables("DATA")
                Set Fields = ReadTableFunc.Tables("FIELDS")
                If IsObject(Table) And IsObject(Fields) Then
                  RowCount = Fields.RowCount()
                  '-Print head line-------------------------------------
                  Set tr = HTMLTable.insertRow(0)
                  For i = 1 To RowCount
                    Set td = tr.insertCell()
                    td.innerText = Fields.Value(i, "FIELDNAME")
                  Next
                  RowCount = Table.RowCount()
                  '-Print SAP table content to HTML table---------------
                  If RowCount > 0 Then
                    For i = 1 To RowCount
                      Set DataLine = _
                        Document.createTextNode(Table.Value(i, "WA"))
                      arrDataLine = Split(DataLine.data, "~")
                      Set tr = HTMLTable.insertRow(i)
                      For j = 0 To UBound(arrDataLine)
                        Set td = tr.insertCell()
                        If Trim(arrDataLine(j)) = "" Then
                          td.innerHTML = " "
                        Else  
                          td.innerText = arrDataLine(j)
                        End If  
                      Next
                    Next
                   Set td = Nothing
                   Set tr = Nothing
                  Else
                    MsgBox "No data", vbOKOnly, "Information"
                  End If
                  Set Table = Nothing
                Else
                  MsgBox "No SAP table instance", vbOKOnly, "Error"
                End If
              Else
                MsgBox "RFC_READ_TABLE " & SAPTableName & _
                  " not successful", vbOKOnly, "Error"
              End If

            Set HTMLTable = Nothing
            Set ReadTableFunc = Nothing

          Else
            MsgBox "No RFC_READ_TABLE instance", vbOKOnly, "Error"
          End If

          '-Logoff------------------------------------------------------
          Connection.Logoff()

        Else
          MsgBox "No connection to SAP system", vbOKOnly, "Error"
        End If

      End Sub

    </script>

  </head>

  <!-- GUI---------------------------------------------------------- -->

  <body>

    <h2 style="font-family:Arial;">
      Connect a SAP system and view table content
    </h2>

    <form name="eingabe" style="font-family:Arial;">
      <!-- Client (Mandant) -->
      Client: <input type="text" name="Client" size="3" value="001">
        
      <!-- User (Benutzer) -->
      User: <input type="text" name="User" size="15" value="BCUSER">
        
      <!-- Password -->
      Password: <input type="password" name="Password" size="25"
        value="minisap">
      <br />
      <!-- System (SID) -->
      System: <input type="text" name="System" size="3" value="NSP">
        
      <!-- Hostname -->
      Hostname: <input type="text" name="HostName" size="25"
        value="ABAP">
        
      <!-- Systemnumber -->
      Systemnumber: <input type="text" name="SystemNumber" size="2"
        value="00">
      <br /><br />
      <!-- SAPTablename -->
      Table name: <input type="text" name="SAPTableName" size="30">
       
      <!-- Button to view the table -->
      <input type="button" value="GetData" onClick='GetData()'>
    </form>

    <table id="Tabelle" border="1">
    </table>

  </body>

</html>

<!-- End------------------------------------------------------------ -->