Skip to Content
Technical Articles
Author's profile photo Stefan Schnell

How to use dotNET Languages Inside SAP GUI Scripting or GuiXT

The basically way to use dotNET languages I describe here.

Here is now an alternative to use dotNET library source files direct inside SAP GUI Scripting.

At first a source code of a Visual Basic dotNET library file:


    Option Strict On

    Imports System
    Imports Microsoft.VisualBasic

    Namespace VBCode

      Public Class VB

        Public Shared Function Hello1() As String
          Return "Hello World!"
        End Function

        Public Function Hello2(ByVal Name As String) As String
          Return "Hello " & Name & "!"
        End Function

        Public Sub Hello3(ByVal Name As String)
          MsgBox(Name, MsgBoxStyle.OkOnly, "Hello")
        End Sub

      End Class

    End Namespace



You can compile this code via

vbc /target:library /out:MyLib.dll MyLib.vb

to a library an use it inside your VB projects.


Also you can use this code direct inside VBScript resp. SAP GUI Scripting:


    Option Explicit
    On Error Resume Next

    Const OUTPUT_BUFFER = 2
    Const ForReading = 1
    Const FileName = "MyLib.vb"

    Dim FSO, File, VBCode
    Dim PS, PSCode, PSOutput


    '-Read the VBSharp code---------------------------------------------
      Set FSO = CreateObject("Scripting.FileSystemObject")
      If IsObject(FSO) Then
        Set File = FSO.OpenTextFile(FileName, ForReading)
        VBCode = File.ReadAll
        Set FSO = Nothing
        MsgBox "Can't create FileSystemObject", vbOkOnly, _
          "Important hint"
      End If

    If VBCode <> "" Then

      Set PS = CreateObject("SAPIEN.ActiveXPoSH")
      If IsObject(PS) Then

        PS.OutputMode = OUTPUT_BUFFER

        If PS.Init(vbFalse) = 0 And PS.IsPowerShellInstalled() <> 0 Then

          PSCode = "$VBCode = @""" & VBCode & """@;"
          PSCode = PSCode & vbCrLf
          PSCode = PSCode & "Add-Type -TypeDefinition $VBCode " & _
            "-Language VisualBasic"
          PSCode = PSCode & vbCrLf
          PSCode = PSCode & "$VB = new-Object VBCode.VB"
          PSCode = PSCode & vbCrLf

          '-Execute the different modules-------------------------------
            PSCode = PSCode & "[VBCode.VB]::Hello1()"
            PSCode = PSCode & vbCrLf
            PSCode = PSCode & "$VB.Hello2(""Stefan"")"
            PSCode = PSCode & vbCrLf
            PSCode = PSCode & "$VB.Hello3(""Stefan"")"

          MsgBox PS.OutputString(), vbOkOnly, "VB# results"

        End  If
        Set PS = Nothing
        MsgBox "Can't create ActiveXPoSH", vbOkOnly, "Important hint"
      End If

    End If



At first we read the source of the library in the variable VBCode. Now we insert this content in the context of the PowerShell script code inside the variable PSCode. At last we execute the code and it runs perfect.

Now it is very easy to code dotNET language classes and to use this classes multiple, e.g. in your dotNET projects and inside SAP GUI Scripting.

Also you can use this way with GuiXT. GuiXT offers with the commands CallVBS and CallVBSAsync full access to VBScript. With GuiXT you can change the SAP Screens without any modifications in the SAP backend. The combination between GuiXT, SAP GUI Scripting and Sapiens PowerShell bridge to the dotNET functionalities offers new horizons of possiblities on presentation server development.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.