ActiveXコントロールを組み込む
GuiXTコントロールを使用すると、SAP GUIウィンドウにActiveXコントロールを組み込んで、VB.NETを介してコントロールを自動化することができます。Windows標準のすべてのコントロールを使用することができます(HTMLコントロールまたはWindowsコモンコントロールのリストビューやツリーコントロールなど)。さらに、追加のActiveXコントロールがMicrosoftから無償で提供されています(リッチテキストコントロールなど)。また、インターネット上で数多くのコントロールを入手できます(PDFの表示、スプレッドシート、グラフィックス、CAD、電話接続など)。
このチュートリアルでは、VB.NET経由で接続される2つのコントロールを扱います。
- RTFコントロール
SAPデータを含むドキュメントを表示します。ユーザーは、ドキュメントを変更して新しい内容を保存できます。 - HTMLコントロール
HTMLコントロールを使用して、ドキュメントの書式設定(太字、下線、イタリック、箇条書き、色)が可能なツールバーを実装します。
ここでは、HTMLドキュメントのクリック要素を処理して、選択したアクションをRTFドキュメントに適用するというのが興味深いポイントです。

トランザクションVA23のドキュメントとツールボックス
このドキュメントはRTFテンプレートを用いて作成し、RTFコントロールで表示しています。ツールボックスはHTMLで実装しています。

ユーザーは、テキストの一部をマウスで選択し、ツールボックスにあるいずれかの書式設定ボタンをクリックできます。

RTFコントロールにおけるテキストの選択

ツールバーボタンクリックして、アドレスを“太字“の“水色“に設定
ユーザーは、テキストの書式を変更し、その書式を保存できます。

これで、このテキストをEメールで送信したり、SAPシステムに保存したり(オブジェクトサービス)、ローカルで処理(MS Wordなどで)したりできます。

実装の主な部分は以下のとおりです。
SAP GUIウィンドウに2つのコントロールを組み込む(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”
VB.NET関数にてコントロールを初期化する(GuiXT Script)
// initialize control in VB
CallVB “utilities.customer.rtf_init” “&V[rtf]” “&V[colorpicker]”
“rtfcontent”
RTFドキュメントを読み込む(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)
ツールボックスのクリック要素を処理する(VB.NET、初期化)
‘ set handler for HTML click
Dim doc As mshtml.HTMLDocumentClass = html.Document
AddHandler CType(doc, mshtml.HTMLDocumentEvents2_Event).onclick, AddressOf
WebBrowserClick
クリックされた要素を判定する(VB.NET、Webブラウザーのクリック)
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”
…
テキストの書式を変更する(VB.NET、Webブラウザーのクリック)
Case “bold”
‘ set/delete “bold”
If IsDBNull(RTFBox.SelBold) OrElse RTFBox.SelBold = False Then
RTFBox.SelBold = True
Else
RTFBox.SelBold = False
End If
…
テキストの色を変更する(VB.NET、Webブラウザーのクリック)
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
変更されたRTFテキストをGuiXTロングテキスト変数に読み込む(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
RTFドキュメントを保存する(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
全体のVB.NETコード
|
1 |
Imports guinet |
| 2 | Imports SAPFEWSELib |
| 3 | Imports RichTextLib |
| 4 | Imports SHDocVw |
| 5 | |
| 6 | Public Class customer |
| 7 | |
| 8 | Private RTFBox As RichTextLib.RichTextBox |
| 9 | |
| 10 | Public Sub rtf_init(rtf As RichTextLib.RichTextBox, _ |
| 11 | html As SHDocVw.WebBrowser, textname As String) |
| 12 | |
| 13 | ‘ Appearence |
| 14 | rtf.BorderStyle = BorderStyleConstants.rtfNoBorder |
| 15 | rtf.BackColor = RGB(255, 255, 255) |
| 16 | |
| 17 | |
| 18 | ‘ load content from GuiXT long text |
| 19 | Dim guixt As New guinet.guixt |
| 20 | rtf.TextRTF = guixt.GetText(textname) |
| 21 | |
| 22 | ‘ set handler for HTML click |
| 23 | Dim doc As mshtml.HTMLDocumentClass = html.Document |
| 24 | AddHandler CType(doc, mshtml.HTMLDocumentEvents2_Event).onclick, _ |
| 25 | AddressOf WebBrowserClick |
| 26 | |
| 27 | ‘ Save RTF object in order to find it later on in click handler |
| 28 | RTFBox = rtf |
| 29 | |
| 30 | End Sub |
| 31 | |
| 32 | Public Sub rtf_get_content(rtf As RichTextLib.RichTextBox, _ |
| 33 | textname As String) |
| 34 | |
| 35 | ‘ read content into GuiXT long text |
| 36 | Dim guixt As New guinet.guixt |
| 37 | guixt.SetText(textname, rtf.TextRTF) |
| 38 | |
| 39 | End Sub |
| 40 | |
| 41 | Private Function WebBrowserClick(pEvtObj As mshtml.IHTMLEventObj) As Boolean |
| 42 | |
| 43 | |
| 44 | ‘ where did the user click? |
| 45 | Dim clickedElement As mshtml.IHTMLElement = pEvtObj.srcElement |
| 46 | |
| 47 | ‘ search table cell in DOM hierarchy |
| 48 | While clickedElement IsNot Nothing _ |
| 49 | AndAlso clickedElement.tagName.ToUpper <> “TD” |
| 50 | clickedElement = clickedElement.parentElement |
| 51 | End While |
| 52 | |
| 53 | ‘ table cell found? else return |
| 54 | If clickedElement Is Nothing Then |
| 55 | Return False |
| 56 | End If |
| 57 | |
| 58 | |
| 59 | |
| 60 | Dim doc As mshtml.HTMLDocumentClass = clickedElement.document |
| 61 | |
| 62 | Select Case clickedElement.id |
| 63 | |
| 64 | Case “bold” |
| 65 | ‘ set/delete “bold” |
| 66 | If IsDBNull(RTFBox.SelBold) OrElse RTFBox.SelBold = False Then |
| 67 | RTFBox.SelBold = True |
| 68 | Else |
| 69 | RTFBox.SelBold = False |
| 70 | End If |
| 71 | |
| 72 | Case “underline” |
| 73 | ‘ set/delete “underline” |
| 74 | If IsDBNull(RTFBox.SelUnderline) _ |
| 75 | OrElse RTFBox.SelUnderline = False Then |
| 76 | RTFBox.SelUnderline = True |
| 77 | Else |
| 78 | RTFBox.SelUnderline = False |
| 79 | End If |
| 80 | |
| 81 | Case “italic” |
| 82 | ‘ set/delete “italic” |
| 83 | If IsDBNull(RTFBox.SelItalic) OrElse RTFBox.SelItalic = False Then |
| 84 | RTFBox.SelItalic = True |
| 85 | Else |
| 86 | RTFBox.SelItalic = False |
| 87 | End If |
| 88 | |
| 89 | Case “bullet” |
| 90 | ‘ set/delete “bullet” |
| 91 | If IsDBNull(RTFBox.SelBullet) OrElse RTFBox.SelBullet = False Then |
| 92 | RTFBox.SelBullet = True |
| 93 | Else |
| 94 | RTFBox.SelBullet = False |
| 95 | End If |
| 96 | |
| 97 | |
| 98 | Case Else |
| 99 | Dim selectedColorRGB As String = _ |
| 100 | pEvtObj.srcElement.style.backgroundColor |
| 101 | |
| 102 | If Not selectedColorRGB.StartsWith(“rgb(“) Then |
| 103 | Return False |
| 104 | End If |
| 105 | |
| 106 | ‘ convert RGB color into integer |
| 107 | Dim selectedColorInt As Integer = RGBStringToInt(selectedColorRGB) |
| 108 | |
| 109 | ‘ set/delete color |
| 110 | If IsDBNull(RTFBox.SelColor) _ |
| 111 | OrElse RTFBox.SelColor <> selectedColorInt Then |
| 112 | RTFBox.SelColor = selectedColorInt |
| 113 | Else |
| 114 | RTFBox.SelColor = RGB(0, 0, 0) |
| 115 | End If |
| 116 | |
| 117 | End Select |
| 118 | |
| 119 | Return True |
| 120 | End Function |
| 121 | |
| 122 | Function RGBStringToInt(rgbstring As String) As Integer |
| 123 | |
| 124 | ‘ color string format is e.g. rgb(128,16,255) |
| 125 | ‘ remove “rgb(” And “)” |
| 126 | rgbstring = rgbstring.Substring(4).Trim(“)”) |
| 127 | |
| 128 | Dim rgbcomponents() As String = rgbstring.Split(“,”c) |
| 129 | |
| 130 | Dim R As Integer = CInt(rgbcomponents(0)) |
| 131 | Dim G As Integer = CInt(rgbcomponents(1)) |
| 132 | Dim B As Integer = CInt(rgbcomponents(2)) |
| 133 | |
| 134 | ‘ convert RGB color into integer |
| 135 | Return ColorTranslator.ToWin32(Color.FromArgb(R, G, B)) |
| 136 | |
| 137 | End Function |
| 138 | |
| 139 | End Class |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 |
すべてのファイルをzip形式でダウンロードすることができます。zip形式の全ファイル
実装に関するいくつかの追記事項を以下に示します。
VB.NET用ActiveXインターフェイスのドキュメント
Microsoftのコントロールについては、インターネット(通常、MSDN)上でドキュメントを見つけることができます。今回の例に関するドキュメントはVisual Basic: RichTextBoxコントロールです。メソッドはVB6用に書かれていますが、このメソッドはVB.NETでもVB6と同じように使用できます。
RTFコントロールでスクロールバーを表示する方法
通常、組み込んだコントロールの表示属性は、自分が作成したVB.NET初期化関数で設定することができます。今回の例ではrtf_initメソッドです。RTFコントロールのScrollBarsのプロパティは実行時に設定できないため、初期化関数はこのプロパティに対しては機能しません。Microsoftのドキュメントでは以下のように記述されています。
ScrollBarsプロパティ(RichTextBoxコントロール)
RichTextBoxコントロールが水平または垂直のどちらのスクロールバーを持つかを返します(または設定します)。実行時は読み取り専用です。
構文
object.ScrollBars
objectプレースフォルダーは、RichTextBoxコントロールとして評価されるオブジェクト式を表します。
設定
ScrollBarsプロパティの設定は以下のとおりです。
| 定数 | 値 | 説明 |
|---|---|---|
| rtfNone | 0 | (デフォルト)スクロールバーを表示しません。 |
| rtfHorizontal | 1 | 水平スクロールバーのみ |
| rtfVertical | 2 | 垂直スクロールバーのみ |
| rtfBoth | 3 | 水平スクロールバーと垂直スクロールバーを両方表示します。 |
ただし今回の例では、プロパティを設定するもうひとつの方法があります。Controlステートメントのproperties=オプションを使用します。
Control (2,0) (20,72) name=“rtf” … properties=“ScrollBars:3”
なお、プロパティ名は大文字と小文字が区別されます。すなわち、scrollbar:3は機能せず、GuiXTの構文エラーメッセージが表示されます。
ユーザーのPCへのActiveXコンポーネントのロールアウト
これは、標準のWindowsコントロールでは必要ありません。個別に購入したコンポーネントには、通常、各PCで実行する必要があるセットアッププログラムが付属しています。
今回の例(RTFコントロール)では、このコントロールがWindows 8および64bit版を含むすべてのバージョンのWindowsで使用できることをMicrosoftが保証しています。ただし、このコントロールは各バージョンのWindowsに自動的にはインストールされません。詳細は、Windows Vista、Windows Server 2008、Windows 7、Windows 8におけるVisual Basic 6.0のサポートに関するMicrosoftの声明を参照してください。
64bit版Windows 7などでは、以下の操作が必要な場合があります。
- “richtx32.ocx”をC:\Windows\SysWoW64にコピーします。
- “管理者として実行”にてMS DOSウィンドウ(コマンドプロンプト)を開きます。
- 以下のコマンドを実行します。
C:\Windows\SysWoW64\regsvr32.exe richtx32.ocx
各ユーザーに対し、Windowsのログオフを使用してこのインストール手順を実行することができます。

