With GuiXT
Controls you can embed ActiveX controls into the SAP GUI window and
automate the controls via VB.NET. All
Windows standard controls are possible, e.g. the HTML control or the
Windows common controls list view and tree control. In addition, there
are further ActiveX controls provided by Microsoft without charge,
e.g. the Rich Text control. And there are plenty of controls available
in the internet, e.g. PDF display, spreadsheets, graphics, CAD, telephone connection.
In this
tutorial we work with two controls linked via VB.NET:
RTF control
We display a document that contains SAP data. The user can change
the document and save the new content.
HTML-Control We use an HTML control for the implementation of a
tool bar that allows the user to format the document (bold,
underline, italic, bullets, colors).
An interesting
aspect is that we can handle the click element of the HTML document and
apply the selected action to the RTF document.
Document and toolbox in
transaction VA23
The document
is created via an RTF template and then displayed in the RTF control.
The toolbox is implemented in HTML:
The user may
select a part of the text with the mouse and then click on one of the
formatting buttons in the toolbox:
Text selection in
the RTF control
By clicking on
the toolbar buttons we set the address to "bold" and to color "light
blue"
The user may
change and format the text, and then save it:
Now the text
may be sent as e-mail, stored in the SAP system (object services) or
processed localy e.g. in MS Word:
The main parts
of the implementation are the following:
Embedding the two controls into the SAP GUI window (GuiXT Script) GuiXT
Control (2,0) (20,72) name="rtf" _
progID="RICHTEXT.RichtextCtrl" initFlag="initcontrol" properties="ScrollBars:3"
Control (0.8,49) (1.9,72) name="colorpicker" progID="file://colorpicker.html"
Initializing the controls in a VB.NET function
(GuiXT Script) GuiXT
// initialize control in VB
CallVB "utilities.customer.rtf_init" "&V[rtf]" "&V[colorpicker]" "rtfcontent"
Loading the
RTF document (VB.NET, initialization) VB.NET
Public Sub rtf_init(rtf As RichTextLib.RichTextBox,
html As SHDocVw.WebBrowser, textname As String)
' load content from GuiXT long text
Dim guixt As New guinet.guixt
rtf.TextRTF = guixt.GetText(textname)
Handling the click element of the toolbox (VB.NET, initialization)
' set handler
for HTML click VB.NET
Dim doc As mshtml.HTMLDocumentClass = html.Document
AddHandler CType(doc, mshtml.HTMLDocumentEvents2_Event).onclick,
AddressOf WebBrowserClick
Determining the clicked element (VB.NET, Web browser click)
VB.NET
Private Function WebBrowserClick(pEvtObj As mshtml.IHTMLEventObj) As Boolean
' where did the user click?
Dim clickedElement As mshtml.IHTMLElement = pEvtObj.srcElement
While clickedElement IsNot Nothing AndAlso clickedElement.tagName.ToUpper <> "TD"
clickedElement = clickedElement.parentElement
End While
Select Case clickedElement.id
Case "bold"
...
Case "underline"
...
Changing the text format (VB.NET, Web browser click) VB.NET
Case "bold"
' set/delete "bold"
If IsDBNull(RTFBox.SelBold) OrElse RTFBox.SelBold = False Then
RTFBox.SelBold = True
Else
RTFBox.SelBold = False
End If
...
Changing the text color (VB.NET, Web browser click)
VB.NET
Dim selectedColorRGB As String = pEvtObj.srcElement.style.backgroundColor
' convert RGB color into integer
Dim selectedColorInt As Integer = RGBStringToInt(selectedColorRGB)
' set/delete color
If IsDBNull(RTFBox.SelColor) OrElse RTFBox.SelColor <> selectedColorInt Then
RTFBox.SelColor = selectedColorInt
Else
RTFBox.SelColor = RGB(0, 0, 0)
End If
Reading the modified RTF text
into a GuiXT long text variable (VB.NET) VB.NET
Public Sub rtf_get_content(rtf As RichTextLib.RichTextBox,
textname As String)
' read content into GuiXT long text
Dim guixt As New guinet.guixt
guixt.SetText(textname, rtf.TextRTF)
End Sub
Saving the RTF document InputScript)
GuiXT
CallVB "utilities.customer.rtf_get_content" _
"&V[rtf]" "rtfcontent"
CopyText fromText="rtfcontent" toFile="&V[outputfile]"
if Q[ok]
Message "S: Text saved" -statusline
else
Message "E: Text not saved" -statusline
endif
Return
Complete VB.NET
coding
VB.NET
Imports guinet
Imports SAPFEWSELib
Imports RichTextLib
Imports SHDocVw
Public Class customer
Private RTFBox As RichTextLib.RichTextBox
Public Sub rtf_init(rtf As RichTextLib.RichTextBox, _
html As SHDocVw.WebBrowser, textname As String)
' Appearence
rtf.BorderStyle = BorderStyleConstants.rtfNoBorder
rtf.BackColor = RGB(255, 255, 255)
' load content from GuiXT long text
Dim guixt As New guinet.guixt
rtf.TextRTF = guixt.GetText(textname)
' set handler for HTML click
Dim doc As mshtml.HTMLDocumentClass = html.Document
AddHandler CType(doc, mshtml.HTMLDocumentEvents2_Event).onclick, _
AddressOf WebBrowserClick
' Save RTF object in order to find it later on in click handler
RTFBox = rtf
End Sub
Public Sub rtf_get_content(rtf As RichTextLib.RichTextBox, _
textname As String)
' read content into GuiXT long text
Dim guixt As New guinet.guixt
guixt.SetText(textname, rtf.TextRTF)
End Sub
Private Function WebBrowserClick(pEvtObj As mshtml.IHTMLEventObj) As Boolean
' where did the user click?
Dim clickedElement As mshtml.IHTMLElement = pEvtObj.srcElement
' search table cell in DOM hierarchy
While clickedElement IsNot Nothing _
AndAlso clickedElement.tagName.ToUpper <> "TD"
clickedElement = clickedElement.parentElement
End While
' table cell found? else return
If clickedElement Is Nothing Then
Return False
End If
Dim doc As mshtml.HTMLDocumentClass = clickedElement.document
Select Case clickedElement.id
Case "bold"
' set/delete "bold"
If IsDBNull(RTFBox.SelBold) OrElse RTFBox.SelBold = False Then
RTFBox.SelBold = True
Else
RTFBox.SelBold = False
End If
Case "underline"
' set/delete "underline"
If IsDBNull(RTFBox.SelUnderline) _
OrElse RTFBox.SelUnderline = False Then
RTFBox.SelUnderline = True
Else
RTFBox.SelUnderline = False
End If
Case "italic"
' set/delete "italic"
If IsDBNull(RTFBox.SelItalic) OrElse RTFBox.SelItalic = False Then
RTFBox.SelItalic = True
Else
RTFBox.SelItalic = False
End If
Case "bullet"
' set/delete "bullet"
If IsDBNull(RTFBox.SelBullet) OrElse RTFBox.SelBullet = False Then
RTFBox.SelBullet = True
Else
RTFBox.SelBullet = False
End If
Case Else
Dim selectedColorRGB As String = _
pEvtObj.srcElement.style.backgroundColor
If Not selectedColorRGB.StartsWith("rgb(") Then
Return False
End If
' convert RGB color into integer
Dim selectedColorInt As Integer = RGBStringToInt(selectedColorRGB)
' set/delete color
If IsDBNull(RTFBox.SelColor) _
OrElse RTFBox.SelColor <> selectedColorInt Then
RTFBox.SelColor = selectedColorInt
Else
RTFBox.SelColor = RGB(0, 0, 0)
End If
End Select
Return True
End Function
Function RGBStringToInt(rgbstring As String) As Integer
' color string format is e.g. rgb(128,16,255)
' remove "rgb(" And ")"
rgbstring = rgbstring.Substring(4).Trim(")")
Dim rgbcomponents() As String = rgbstring.Split(","c)
Dim R As Integer = CInt(rgbcomponents(0))
Dim G As Integer = CInt(rgbcomponents(1))
Dim B As Integer = CInt(rgbcomponents(2))
' convert RGB color into integer
Return ColorTranslator.ToWin32(Color.FromArgb(R, G, B))
End Function
End Class
A few additional remarks concerning
implementation:
Documentation of the ActiveX
interface for VB.NET
For the Microsoft controls you can find the documentation in internet,
usually in MSDN (Microsoft Developer Network). In our case it is the
documentation
Visual Basic: RichTextBox Control. The methods are descibed for VB6
an can be used in the same way in VB.NET.
How to display scrollbars in the RTF control Normally the display attributes of the embedded control can be set
in your VB.NET initialization function; in our case this is the method rtf_init.
This does not work for the ScrollBars property of the RTF control
since it cannot be set at runtime. The Microsoft
documentation
states:
ScrollBars Property (RichTextBox
Control)
Returns
or sets a value indicating whether a RichTextBox control
has horizontal or vertical scroll bars. Read-only at
run time.
Syntax
object.ScrollBars
The object placeholder represents
an object expression that
evaluates to a RichTextBox control.
Settings
The ScrollBars property settings
are:
Constant
Value
Description
rtfNone
0
(Default) No scroll bars
shown.
rtfHorizontal
1
Horizontal scroll bar
only.
rtfVertical
2
Vertical scroll bar only.
rtfBoth
3
Both horizontal and
vertical scroll bars shown.
But there is another way to
set the properties of the control in this case:
We use the
properties=
option of the
Control
statement:
Control
(2,0)
(20,72)
name="rtf"
...
properties="ScrollBars:3"
Note that the property names
are case sensitive. i.e. scrollbar:3 will not work and
results in a GuiXT syntax error message.
Rollout of the ActiveX components to the user PCs
This is not necessary for standard Windows controls. Components that you
have bought separately usually include a setup program that needs to run
on each PC .