<!-- 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

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



      Connect a SAP system and view table content

    <hta:application applicationname="ReadSAPTable" id="ReadSAPTable"

    <!-- 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 = _

          '-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
                  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----------------------------------------  
                '-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")
                      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 = "&nbsp;"
                                td.innerText = arrDataLine(j)
                              End If  
                         Set td = Nothing
                         Set tr = Nothing
                          MsgBox "No data", vbOKOnly, "Information"
                        End If
                        Set Table = Nothing
                      MsgBox "No SAP table instance", vbOKOnly, "Error"
                    End If
                    MsgBox "RFC_READ_TABLE " & SAPTableName & _
                      " not successful", vbOKOnly, "Error"
                  End If
              Set HTMLTable = Nothing
              Set ReadTableFunc = Nothing

              MsgBox "No RFC_READ_TABLE instance", vbOKOnly, "Fehler"
            End If


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

        End Sub



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


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

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

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



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