Instead of
implementing your own ActiveX component in VB.NET you can embed a
Windows form into a SAP GUI screen using the
guixt.EmbedForm()
function.
When you
store your VB.NET dll in a central location such as SAP Web Repository
or Mime Repository no installation or registration will be needed on the
user's PC. GUiXT loads the dll and stores it in a local cache where it can
be executed.
In this
tutorial we demonstrate the embedding technique using an example
that displays customer sales information. The example is relatively
extensive and contains various techniques that can be useful for applications of this kind.
In Visual
Studio the design of our form looks as follows:
Visual Studio, Forms Designer
The form
contains various data grids, buttons and a chart. In SAP GUI the user
interface is at follows:
Enter a customer number
and press the "Start"" button
The customer info is
displayed , offering several user interactions
The table at
the upper right hand side contains the products sold during the past 5
years, in descending order of total sales value. By clicking on a
product line you can display the sales chart for the selected product:
Sales chart for selected
product
This action is implemented in VB.NET without requiring further database
access. There are other user interactions that result in executing an
InputScript, for example the "Fidelity 3 years" button which
generates a customer order for the fidelity package.
"Fidelity 3 years"
calls up VA01 and enters the selected package.
The whole
implementation is a joint effort of GuiXT and VB.NET: the
database access and the SAP GUI actions are carried out by GuiXT and the
user interface, with data grids and the chart, are done in VB.NET.
By using the
standard .NET controls we get a couple of features thrown in, for example
ordering a column by clicking on its header:
Order by product name
Our customer
sales dashboard can be embedded into any SAP transaction. Here we have
created a separate demo transaction "ZCSO" as a parameter
transaction for an existing SAP transaction; "Start_Report" in our
example, which is sufficient for demo purposes.
You find all
scripts and the whole VB.NET project in zcso.zip.
If you want to install it for your own tests you will also need the free MS
Chart package which is not auomatically installed in Windows. We have included
the setup file in zcso.zip. Use Visual Studio 2012 or higher. After downloading
the file zcso.zip it will often be necessary to "unblock" it (right mouse click
-> File attributes -> Unblock), otherwise the execution of the files will be
blocked by Windows later on. Or unblock each file separately after unziping the
files.
GuiXT Skript
GuiXT
// ZCSO
// demo transaction of embedded Windows Forms
// delete existing elements (necessary if implemented as parameter transaction)
del (0,0) (20,120)
// initial screen?
if not V[zcso_main_screen=X]
// title
Title "Customer Sales Order Dashboard"
InputField (1,1) "Customer" (1,20) size=10 _
name="zcso_kunnr_ext" techName="KNA1-KUNNR" -numerical
On "Enter" process="zcso_init_enter.txt"
Pushbutton (toolbar) "Start" "/0" "F8" process="zcso_init_enter.txt"
else
// title
Title "&V[zcso_kunnr_ext]: &V[zcso_name1], &V[zcso_city], &V[zcso_country]"
// area for VB.NET control.
// We start at (-1,-1) and use a very large area in order to avoid any padding
Control (-1,-1) (50,300) progID="Shell.Explorer" name="r1" initFlag="r1init"
// embed control
if V[r1init]
CallVB charts.zcso.embed "&V[r1]"
endif
Pushbutton (toolbar) "Other customer" process="zcso_main_back.txt"
Pushbutton (toolbar) "Display customer data (XD03)" _
process="zcso_display_customer.txt"
On "Enter" process="zcso_main_enter.txt"
On "/3" process="zcso_main_back.txt"
endif
InputScipt zcso_init_enter.txt
GuiXT
// customer number specified?
if not V[zcso_kunnr_ext]
return "E: Please specify a customer number" -statusline
endif
// customer number with 10 digits
Set V[zcso_kunnr](1-10) "0000000000&V[zcso_kunnr_ext]" -alignRight
// set organization key values (option:read from user parameters)
Set V[zcso_vkorg] "1000"
Set V[zcso_vtweg] "10"
Set V[zcso_spart] "00"
// prepare VB.NET form
StatusMessage title="Processing..."
CallVB charts.zcso.initialize
// read customer address
StatusMessage addString="1 Address"
include "zcso_read_address.txt"
// read contacts
StatusMessage addString="2 Contacts"
include "zcso_read_contacts.txt"
// read orders
StatusMessage addString="3 Open orders"
include "zcso_read_orders.txt"
// read diagram data from S001
StatusMessage addString="4 Sales figures"
include "zcso_read_diagramdata.txt"
// build tables, diagrams etc.
CallVB charts.zcso.display
// display main screen
Set V[zcso_main_screen] "X"
StatusMessage -remove
Return
InputScipt zcso_read_address.txt
GuiXT
// read customer address
Call "/guixt/select" cache="session" in.table="KNA1" _
in.fields="NAME1,ORT01,LAND1" in.condition="KUNNR = '&V[zcso_kunnr]'" _
out.found="found" out.v1="zcso_name1" _
out.v2="zcso_city" out.v3="zcso_countrycode"
// found?
if not V[found]
StatusMessage -remove
return "E: Customer &V[zcso_kunnr_ext] not found" -statusline
endif
// read country text
Call "/guixt/select" cache="session" in.table="T005T" _
in.fields="LANDX50" _
in.condition="SPRAS = '&V[_language]' AND LAND1 = '&V[zcso_countrycode]'" _
out.v1="zcso_country"
InputScipt zcso_read_contacts.txt
GuiXT
// Build key table for function
Set V[crwa] ""
Set V[crwa](BAPICUSTOMER_IDRANGE-SIGN) "I"
Set V[crwa](BAPICUSTOMER_IDRANGE-OPTION) "EQ"
Set V[crwa](BAPICUSTOMER_IDRANGE-LOW) "&V[zcso_kunnr]"
Set text[cr] "&V[crwa]"
// clear result table
Set text[contacts] ""
// call function
Call "BAPI_CUSTOMER_GETCONTACTLIST" table.CUSTOMERRANGE="cr" _
table.CONTACTADDRESSDATA="contacts"
// transfer data to VB
Set V[i] 1
label contacts_line
CopyText fromText="contacts" toString="wa" line=&V[i]
if Q[ok]
Set V[lastname] "&V[wa](BAPICONTACT_ADDRESSDATA-LASTNAME)"
Set V[firstname] "&V[wa](BAPICONTACT_ADDRESSDATA-FIRSTNAME)"
Set V[zcso_contactname] "&V[lastname], &V[firstname]"
Set V[zcso_contacttitle] "&V[wa](BAPICONTACT_ADDRESSDATA-FUNCTION)"
Set V[zcso_contactphone] "&V[wa](BAPICONTACT_ADDRESSDATA-TEL1_NUMBR)"
Set V[zcso_contactemail] "&V[wa](BAPICONTACT_ADDRESSDATA-E_MAIL)"
CallVB charts.zcso.newcontact
Set V[i] &V[i] + 1
goto contacts_line
endif
InputScipt zcso_read_orders.txt
GuiXT
// read open orders
// search condition
Set V[condition] "KUNNR = '&V[zcso_kunnr]' and VKORG = '&V[zcso_vkorg]'"
Set V[cond1] "and VTWEG = '&V[zcso_vtweg]'"
Set V[cond2] "and SPART = '&V[zcso_spart]'"
Set V[cond3] "and TRVOG = '0'"
Set V[condition] "&V[condition] &V[cond1] &V[cond2] &V[cond3]"
Set V[condition] "&V[condition] and AUDAT GE '&V[today-365_ymd]'"
// clear result tables
Set text[r1] ""
Set text[r2] ""
Set text[r3] ""
Set text[r4] ""
Set text[r5] ""
// read orders
Call /guixt/select in.table="VBAK" in.Condition="&V[condition]" _
in.Fields="VBELN,AUDAT,AUART,NETWR,BSTNK" table.V1table="r1" _
table.V2table="r2" table.V3table="r3" table.V4table="r4" table.V5table="r5"
// pass to VB
CallVB charts.zcso.neworders
// clear result tables
Set text[r1] ""
Set text[r2] ""
Set text[r3] ""
Set text[r4] ""
Set text[r5] ""
InputScipt zcso_read_diagramdata.txt
GuiXT
// Determine years
Set V[zcso_year0] &V[today_y]
Set V[zcso_year1] &V[zcso_year0] - 1
Set V[zcso_year2] &V[zcso_year0] - 2
Set V[zcso_year3] &V[zcso_year0] - 3
Set V[zcso_year4] &V[zcso_year0] - 4
// search condition
Set V[cond1] "KUNNR = '&V[zcso_kunnr]'"
Set V[cond2] "and VKORG = '&V[zcso_vkorg]'"
Set V[cond3] "and VTWEG = '&V[zcso_vtweg]'"
Set V[cond4] "and SPART = '&V[zcso_spart]'"
Set V[cond5] "and SPMON GE '&V[zcso_year4]01'"
Set V[condition] "&V[cond1] &V[cond2] &V[cond3] &V[cond4] &V[cond5]"
// Clear result tables
Set text[r1] ""
Set text[r2] ""
Set text[r3] ""
// read VIS statistics table S001
Call /guixt/select in.table="S001" in.Condition="&V[condition]" _
in.Fields="MATNR,SPMON,AENETWR" table.V1table="r1" _
table.V2table="r2" table.V3table="r3"
// pass data to VB
CallVB charts.zcso.materialsales
// Clear result tables
Set text[r1] ""
Set text[r2] ""
Set text[r3] ""
// obtain all product numbers from VB
CallVB charts.zcso.products_to_guixt
// read product names
if text[zcso_products]
Set text[ctab] ""
Set V[i] 1
label next
CopyText fromText="zcso_products" toString="matnr" line="&V[i]"
if Q[ok]
if V[i=1]
Set V[condline] "SPRAS = 'E' AND (MATNR = '&V[matnr]'
else
Set V[condline] "OR MATNR = '&V[matnr]'"
endif
CopyText fromString="condline" toText="ctab" -appendLine
Set V[i] &V[i] + 1
goto next
endif
if V[i>1]
Set V[condline] ")"
CopyText fromString="condline" toText="ctab" -appendLine
endif
Call /guixt/select in.table="MAKT" in.fields="MATNR,MAKTX" _
table.conditiontable="ctab" table.V1table="r1" table.V2table="r2"
endif
// product names to VB
CallVB charts.zcso.productnames_from_guixt
InputScipt zcso_create_order.txt
GuiXT
Parameter PROMOTION
Enter "/nVA01"
// Enter order type
Screen SAPMV45A.0101
Set F[VBAK-AUART] "TA"
Enter
// Enter customer number
Screen SAPMV45A.4001
Set F[KUAGV-KUNNR] "&V[zcso_kunnr]"
if U[PROMOTION=1]
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-MABNR,1] "M-20"
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-KWMENG,1] "1"
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-MABNR,2] "PA-1000"
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-KWMENG,2] "1"
endif
if U[PROMOTION=2]
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-MABNR,1] "M-20"
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-KWMENG,1] "2"
endif
if U[PROMOTION=3]
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-MABNR,1] "PA-1000"
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-KWMENG,1] "1"
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-MABNR,2] "M-10"
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-KWMENG,2] "1"
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-MABNR,3] "P-103"
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-KWMENG,3] "1"
endif
Enter
// no delivery proposal?
Screen SAPLATP4.0500
Enter "=WEIT"
// no delivery proposal?
Screen SAPLATP4.0500
Enter "=WEIT"
InputScipt zcso_display_order.txt
GuiXT
if not V[zcso_vbeln]
return "E: Please select an order" -statusline
endif
Enter "/nVA03"
// Display order
Screen SAPMV45A.0102
Set F[VBAK-VBELN] "&V[zcso_vbeln]"
Enter
// Skip Popup
Screen SAPMSDYP.0010
Enter
InputScipt zcso_change_order.txt
GuiXT
if not V[zcso_vbeln]
return "E: Please select an order" -statusline
endif
Enter "/nVA02"
// Display order
Screen SAPMV45A.0102
Set F[VBAK-VBELN] "&V[zcso_vbeln]"
Enter
// Skip Popup
Screen SAPMSDYP.0010
Enter
InputScipt Zcso_display_customer.txt
GuiXT
Enter "/nxd03"
// Display customer
Screen SAPMF02D.7101
Set F[RF02D-KUNNR] "&V[zcso_kunnr]"
Set F[RF02D-VKORG] "&V[zcso_vkorg]"
Set F[RF02D-VTWEG] "&V[zcso_vtweg]"
Set F[RF02D-SPART] "&V[zcso_spart]"
Enter
InputScipt zcso_main_back.txt
GuiXT
// back to initial screen
Set V[zcso_main_screen] ""
Return
VB.NET
Imports guinet
Imports System.Windows.Forms
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms.DataVisualization.Charting
Public Class zcso
#Region "Private data"
' interface to GuiXT
Private myguixt As guixt
' Dictionary for sales per product and month
Private matdict1 As New Dictionary(Of String, Decimal)
' Dictionary for sales per product
Private matdict2 As New Dictionary(Of String, Decimal)
' Dictionary for product names
Private matdict3 As New Dictionary(Of String, String)
#End Region
' embed into SAP GUI window
Public Sub embed(ByVal w As Object)
guixt.EmbedForm(Me, w)
End Sub
' initialize for new customer data
Public Sub initialize()
' create guixt object for communication with GuiXT
myguixt = New guixt
' clear data grids
contacts.RowCount = 0
orders.RowCount = 0
matbuttons.RowCount = 0
' clear product dictionaries
matdict1.Clear()
matdict2.Clear()
matdict3.Clear()
End Sub
' final display, all customer data are read
Public Sub display()
' sort contacts
contacts.Sort(contacts.Columns(0),
System.ComponentModel.ListSortDirection.Ascending)
' sort orders
orders.Sort(orders.Columns(0),
System.ComponentModel.ListSortDirection.Descending)
' Build Sales chart
GenerateSalesChart("*")
' no selection
contacts.ClearSelection()
orders.ClearSelection()
End Sub
' Pass all product numbers to GuiXT via GuiXT long text variable
Public Sub products_to_guixt()
' Pass product numbers back to GuiXT to obtain product names
myguixt.SetText("zcso_products", String.Join(vbCrLf, matdict2.Keys))
End Sub
' Obtain all product texts from GuiXT via GuiXT long text variables
Public Sub productnames_from_guixt()
Dim products As String() = myguixt.GetText("r1").Split(vbCrLf)
Dim productnames As String() = myguixt.GetText("r2").Split(vbCrLf)
For k As Integer = 0 To products.Length - 1
matdict3.Add(products(k).Trim, productnames(k).Trim)
Next
End Sub
#Region "Event handling"
'Create new standard order
Private Sub buttoncreateorder_Click(sender As Object,
e As EventArgs) Handles buttoncreateorder.Click
myguixt.Input("OK:/0,process=zcso_create_order.txt")
End Sub
'Create new order, promotion 1
Private Sub promotion1_Click(sender As Object,
e As EventArgs) Handles promotion1.Click
myguixt.Input("U[PROMOTION]:1;OK:/0,process=zcso_create_order.txt")
End Sub
'Create new order, promotion 2
Private Sub promotion2_Click(sender As Object,
e As EventArgs) Handles promotion2.Click
myguixt.Input("U[PROMOTION]:2;OK:/0,process=zcso_create_order.txt")
End Sub
'Create new order, promotion 3
Private Sub promotion3_Click(sender As Object,
e As EventArgs) Handles promotion3.Click
myguixt.Input("U[PROMOTION]:3;OK:/0,process=zcso_create_order.txt")
End Sub
' Display customer order
Private Sub buttondisplayyorder_Click(sender As Object,
e As EventArgs) Handles buttondisplayorder.Click
Dim ordernumber As String = ""
For Each row In orders.SelectedRows
ordernumber = row.Cells("ordervbeln").Value()
Next
myguixt.SetVariable("zcso_vbeln", ordernumber)
myguixt.Input("OK:/0,process=zcso_display_order.txt")
End Sub
' Change customer order
Private Sub buttonchangeorder_Click(sender As Object,
e As EventArgs) Handles buttonchangeorder.Click
Dim ordernumber As String = ""
For Each row In orders.SelectedRows
ordernumber = row.Cells("ordervbeln").Value()
Next
myguixt.SetVariable("zcso_vbeln", ordernumber)
myguixt.Input("OK:/0,process=zcso_change_order.txt")
End Sub
' Click on product button to generate chart
Private Sub matbuttons_CellClick(sender As Object,
e As DataGridViewCellEventArgs) Handles matbuttons.CellClick
Dim product As String = ""
For Each row In matbuttons.SelectedRows
product = row.Cells("prodnumber").Value()
Next
GenerateSalesChart(product)
End Sub
#End Region
#Region "contacts"
' called from GuiXT for each new contact
Public Sub newcontact()
Dim k As Integer = contacts.Rows.Add()
Dim c As System.Windows.Forms.DataGridViewCellCollection
= contacts.Rows.Item(k).Cells
c("contactname").Value = myguixt.GetVariable("zcso_contactname")
c("contacttitle").Value = myguixt.GetVariable("zcso_contacttitle")
c("contactphone").Value = myguixt.GetVariable("zcso_contactphone")
c("contactemail").Value = myguixt.GetVariable("zcso_contactemail")
End Sub
#End Region
#Region "orders"
' transfer order data from GuiXT to datagrid
Public Sub neworders()
Dim r1 As String() = myguixt.GetText("r1").Split(vbCrLf)
Dim r2 As String() = myguixt.GetText("r2").Split(vbCrLf)
Dim r3 As String() = myguixt.GetText("r3").Split(vbCrLf)
Dim r4 As String() = myguixt.GetText("r4").Split(vbCrLf)
Dim r5 As String() = myguixt.GetText("r5").Split(vbCrLf)
For k As Integer = 0 To r1.Length - 1
orders.Rows.Add()
Dim o As System.Windows.Forms.DataGridViewCellCollection
= orders.Rows.Item(k).Cells
o("ordervbeln").Value = CInt(r1(k).Trim)
o("orderaudat").Value = r2(k).Trim
o("orderauart").Value = r3(k).Trim
o("ordernetwr").Value = Decimal.Parse(r4(k).Trim,
Globalization.NumberStyles.AllowDecimalPoint).ToString()
o("orderbstnk").Value = r5(k).Trim
Next
End Sub
#End Region
#Region "materials"
Public Sub materialsales()
Dim r1 As String() = myguixt.GetText("r1").Split(vbCrLf)
Dim r2 As String() = myguixt.GetText("r2").Split(vbCrLf)
Dim r3 As String() = myguixt.GetText("r3").Split(vbCrLf)
For k As Integer = 0 To r1.Length - 1
Dim product As String = r1(k).Trim
Dim year As String = r2(k).Trim.Substring(0, 4)
Dim netwr As Decimal = Decimal.Parse(r3(k).Trim)
Dim key As String = year & product
If matdict1.ContainsKey(key) Then
matdict1.Item(key) = matdict1.Item(key) + netwr
Else
matdict1.Add(key, netwr)
End If
' product only
If matdict2.ContainsKey(product) Then
matdict2.Item(product) = matdict2.Item(product) + netwr
Else
matdict2.Add(product, netwr)
End If
Next
End Sub
#End Region
#Region "charts"
Private total_netwr0 As Decimal = 0
Private total_netwr1 As Decimal = 0
Private total_netwr2 As Decimal = 0
Private total_netwr3 As Decimal = 0
Private total_netwr4 As Decimal = 0
Public Sub GenerateSalesChart(product As String)
' create chart
Dim Chart1 As New Chart()
Chart1.Size = New System.Drawing.Size(salesdiagram.Size.Width,
salesdiagram.Size.Height)
Chart1.BackColor = Color.FromArgb(240, 240, 240)
Dim ChartArea1 As New ChartArea
ChartArea1.Name = "ChartArea1"
Chart1.ChartAreas.Add(ChartArea1)
Dim Series1 As New Series
Series1.ChartArea = "ChartArea1"
Series1.Palette = ChartColorPalette.Pastel
Series1.XValueMember = "Year"
Series1.YValueMembers = "Sales"
Chart1.Series.Add(Series1)
Dim table As New DataTable
' Create columns in the DataTable
table.Columns.Add("Year", GetType(String))
table.Columns.Add("Sales", GetType(Integer))
Dim year0 As String = myguixt.GetVariable("zcso_year0")
Dim year1 As String = myguixt.GetVariable("zcso_year1")
Dim year2 As String = myguixt.GetVariable("zcso_year2")
Dim year3 As String = myguixt.GetVariable("zcso_year3")
Dim year4 As String = myguixt.GetVariable("zcso_year4")
Dim netwr0 As Decimal = 0
Dim netwr1 As Decimal = 0
Dim netwr2 As Decimal = 0
Dim netwr3 As Decimal = 0
Dim netwr4 As Decimal = 0
For Each m In matdict1
If product = "*" Or product = "***"
Or product = m.Key.Substring(4) Then
Select Case m.Key.Substring(0, 4)
Case year0
netwr0 += m.Value
Case year1
netwr1 += m.Value
Case year2
netwr2 += m.Value
Case year3
netwr3 += m.Value
Case year4
netwr4 += m.Value
End Select
End If
Next
' Save total values
If product = "*" Then
total_netwr0 = netwr0
total_netwr1 = netwr1
total_netwr2 = netwr2
total_netwr3 = netwr3
total_netwr4 = netwr4
' build matbuttons
Dim total_netwr As Decimal = total_netwr0
+ total_netwr1 + total_netwr2 + total_netwr3 + total_netwr4
matbuttons.Rows.Add()
Dim mb As System.Windows.Forms.DataGridViewCellCollection
= matbuttons.Rows.Item(0).Cells
mb("percentage").Value = "100"
mb("product").Value = "***"
mb("prodnumber").Value = "***"
mb("prodname").Value = "All products"
mb("netwr").Value = total_netwr
' select first row
matbuttons.ClearSelection()
matbuttons.Rows.Item(0).Selected = True
For Each m In matdict2
Dim k As Integer = matbuttons.Rows.Add()
mb = matbuttons.Rows.Item(k).Cells
Dim percentage As Integer
' avoid to divide by 0
If total_netwr = 0 Then
percentage = 0
Else
percentage = CInt(m.Value * 100 / total_netwr)
End If
mb("percentage").Value = percentage.ToString
' Suppress leading 000
If IsNumeric(m.Key) Then
mb("product").Value = CInt(m.Key).ToString
Else
mb("product").Value = m.Key
End If
mb("prodnumber").Value = m.Key
mb("prodname").Value = matdict3.Item(m.Key)
mb("netwr").Value = m.Value
Next
' sortmatbuttons
matbuttons.Sort(matbuttons.Columns("netwr"),
System.ComponentModel.ListSortDirection.Descending)
End If
' Sales figures for 5 years
table.Rows.Add(year4, CInt(netwr4))
table.Rows.Add(year3, CInt(netwr3))
table.Rows.Add(year2, CInt(netwr2))
table.Rows.Add(year1, CInt(netwr1))
table.Rows.Add(year0, CInt(netwr0))
' Chart data
Chart1.DataSource = table
Dim bmp As New Bitmap(Chart1.Width, Chart1.Height)
Dim rec As New Rectangle(0, 0, Chart1.Width, Chart1.Height)
Chart1.DrawToBitmap(bmp, rec)
salesdiagram.Image = bmp
' Title variable
If product = "*" OrElse product = "***" Then
product_selected.Text = "All products"
Else
product_selected.Text = product & " " & matdict3.Item(product)
End If
salesdiagram.Refresh()
End Sub
#End Region
End Class