Technical Articles
Access to SAP via ActiveX control and VBScript
With the installation of SAP GUI for Windows on the presentation server you get also some ActiveX controls to connect and to communicate with a SAP system. You can use these ActiveX controls easily inside Visual Basic Script and therefore inside of a HTML file resp. HTML application – but only with Microsoft Internet Explorer. It is possible to build a HTML GUI and use it with data from a SAP system. Inside the HTML there is a VBScript functionality necessary, which gets the data from the SAP system and uses the Document Object Model (DOM).
In the following source I create an easy GUI. There is only the connection parameters, an input field for a SAP table name and a button to view the SAP table content in the HTML table. So you can view any SAP table content as HTML table.
<!-- 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
-------------------------------------------------------------------- -->
<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">
'-Directives------------------------------------------------------
Option Explicit
'-Sub GetData-----------------------------------------------------
Sub GetData()
'-Variables---------------------------------------------------
Dim SAPFunc, Connection, SAPConnection, ReadTableFunc
Dim Param, Table, RowCount, i, j, DataLine, Node, tmp
Dim Tabelle, TableName, tr, td, arrDataLine, arrFields
Dim FieldName
'-Get the name of the table-----------------------------------
TableName = CStr(Document.eingabe.TableName.Value)
If Trim(TableName) = "" Then
Exit Sub
End If
'-Get SAP.Functions-------------------------------------------
Set SAPFunc = CreateObject("SAP.Functions")
If Not IsObject(SAPFunc) Then
Exit Sub
End If
'-Get SAP.LogonControl connection-----------------------------
Set Connection = SAPFunc.Connection()
If Not IsObject(Connection) Then
Exit Sub
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 = "DE"
Connection.System = CStr(Document.eingabe.System.Value)
Connection.HostName = CStr(Document.eingabe.HostName.Value)
Connection.SystemNumber = _
CStr(Document.eingabe.SystemNumber.Value)
'-Connect SAP system------------------------------------------
SAPConnection = Connection.Logon(0, vbTrue)
If SAPConnection = vbTrue Then
'-Get function module RFC_READ_TABLE------------------------
Set ReadTableFunc = SAPFunc.Add("RFC_READ_TABLE")
If IsObject(ReadTableFunc) Then
'-Clear HTML table----------------------------------------
Set Tabelle = Document.getElementById("Tabelle")
j = Tabelle.childNodes.length
If j > 0 Then
For i = 1 To j
Set Node = Tabelle.lastChild
Tabelle.removeChild(Node)
Set Node = Nothing
Next
End If
'-Define export parameter DELIMITER-----------------------
Set Param = ReadTableFunc.Exports("DELIMITER")
Param.Value = "~"
'-Get header line informations from the SAP table---------
'-Define export parameter QUERY_TABLE-------------------
Set Param = ReadTableFunc.Exports("QUERY_TABLE")
Param.Value = "DD03L"
'-Define export parameter OPTIONS-----------------------
Set Param = ReadTableFunc.Tables("OPTIONS").Rows.Add
Param("TEXT") = "TABNAME = '" & TableName & "'"
'-Define export parameter FIELDS------------------------
Set Param = ReadTableFunc.Tables("FIELDS").Rows.Add
Param("FIELDNAME") = "POSITION"
Set Param = ReadTableFunc.Tables("FIELDS").Rows.Add
Param("FIELDNAME") = "FIELDNAME"
'-Read table--------------------------------------------
If ReadTableFunc.Call() = vbTrue Then
Set Table = ReadTableFunc.Tables("DATA")
If IsObject(Table) Then
RowCount = Table.RowCount()
'-Copy result to an array-------------------------
ReDim arrFields(RowCount - 1)
For i = 0 To RowCount - 1
arrFields(i) = Table.Value(i + 1, "WA")
Next
ReadTableFunc.Tables("DATA").Rows.RemoveAll
Set Table = Nothing
End If
End If
'-Sort array with field names (BubbleSort)--------------
For i = 0 To UBound(arrFields)
For j = i + 1 To UBound(arrFields)
If arrFields(i) > arrFields(j) Then
tmp = arrFields(i)
arrFields(i) = arrFields(j)
arrFields(j) = tmp
End If
Next
Next
'-Print field names in the HTML table-------------------
Set tr = Tabelle.insertRow(0)
For j = 0 To UBound(arrFields)
Set td = tr.insertCell()
FieldName = Split(arrFields(j), "~")
td.innerText = FieldName(1)
Next
'-Get SAP table data--------------------------------------
'-Define export parameter QUERY_TABLE-------------------
Set Param = ReadTableFunc.Exports("QUERY_TABLE")
Param.Value = TableName
'-Delete entries----------------------------------------
ReadTableFunc.Tables("OPTIONS").Rows.RemoveAll
ReadTableFunc.Tables("FIELDS").Rows.RemoveAll
'-Read table--------------------------------------------
If ReadTableFunc.Call() = vbTrue Then
Set Table = ReadTableFunc.Tables("DATA")
If IsObject(Table) Then
RowCount = Table.RowCount()
'-Print SAP table content to HTML table-----------
For i = 1 To RowCount
Set DataLine = _
Document.createTextNode(Table.Value(i, "WA"))
arrDataLine = Split(DataLine.data, "~")
Set tr = Tabelle.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 Table = Nothing
End If
End If
Set td = Nothing
Set tr = Nothing
Set Tabelle = Nothing
Set ReadTableFunc = Nothing
End If
'-Logoff----------------------------------------------------
Connection.Logoff()
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="000">
<!-- 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="192.168.28.139">
<!-- Systemnumber -->
Systemnumber: <input type="text" name="SystemNumber" size="2"
value="00">
<br /><br />
<!-- Tablename -->
Table name: <input type="text" name="TableName" size="30">
<!-- Button to view the table -->
<input type="button" value="GetData" onClick='GetData()'>
</form>
<table id="Tabelle" border="1">
</table>
</body>
</html>
<!-- End------------------------------------------------------------ -->
P.S. Look also here: http://www.stschnell.de/knowcoll/readtableviewhtml.html
Hi Stefan,
Where do I get the Client, SystemNumber? Thanks
Hello ZZy ZX,
you get the SystemNumber from your SAP Logon. Open the connections in Explorer View and scroll to the right side. You find the column Instance Number, this is the SystemNumber.
In a normal case you get the number of the client from your logon screen. Open a connection to your favorite SAP system and you find the client number as first input field. It is preassigned with a standard value. In other cases the client number called Mandant.
Cheers
Stefan
Hi Stefan.
I tried your script and I get and error:
Object doesn't support this property or method: 'Document.eingabe.TableName'
Any ideas?
Thanks, Marc
Hello Marc,
I copied the source from above in a file called test.hta and execute it. In my case it works well with Internet Explorer 10.
Document.eingabe.TableName describes the input field TableName of the form eingabe of the HTML document.
Okay, I see, if I store the document as HTML file, I get the same error. Switch your IE in compatibility mode, now it should work.
Cheers
Stefan