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)
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)
// initialize
control in VB
CallVB
"utilities.customer.rtf_init"
"&V[rtf]" "&V[colorpicker]"
"rtfcontent"
Loading the
RTF document (VB.NET, initialization)
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
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)
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)
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)
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)
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)
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 | 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
|
You can download all
files in zipped format:
All files as zip file
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 .
In our case (RTF control) Microsoft
assures that the control can be used for all Windows versions including Windows 8
and including
64 bit systems. But the control is not automatically installed in each
Windows version; for details see
Microsoft Support Statement for Visual Basic 6.0 on Windows Vista,
Windows Server 2008, Windows 7, and Windows 8.
The following actions may be necessary, e.g.
for 64bit WIndows 7:
-
Copy the file "richtx32.ocx"
to C:\Windows\SysWoW64
-
Open an MS DOS Window (command prompt)
with "Run as Administrator"
-
Execute the command
C:\Windows\SysWoW64\regsvr32.exe richtx32.ocx
You can use a Windows logon exit to carry
out these installation steps for each user.
|