Search code examples
.netexcelvbasap-guisap-dotnet-connector

SAPGUI Parse GuiUserArea


Problem!

Currently at the place that I work We are trying to perform some kind of repetitive task via automatically with the usage of SAPGui, Excel & VBA. Usually, the most of the reports that We gather from SAP's TCodes are being displayed with a GuiUserArea which is not easy, neat and quick to parse.

Solution!

Anyhow I managed to parse these kind of reports depending on certain types of requirements. So, for the first time that I tried to parse any report (shown as GuiUserArea) I've came up with the idea that it would be easy to save the report as unformated text and then go and parse it using VBA (Regexes, Splits, Text Length, ...) instead of using GuiUserArea methods and properties.

Please note that using this procedure (Saving the files) is more easy and quick to parse the information, But what would be the point of using the SAPGUI object just to save the files and not to complete more complex tasks as parsing information...

Working with the GuiUserArea I've came up with the following solution:

Sub ParseSAPGUI()
    Dim objSAPGui As Object
    Dim objApplication As Object
    Dim objConnection As Object
    Dim objSession As Object
    
    If (objSAPGui Is Nothing) Then
        Set objSAPGui = GetSAPGuiObject()
        Set objApplication = GetSAPGuiScriptEngine(objSAPGui)
    End If
    
    If (objConnection Is Nothing) Then
        Set objConnection = GetSAPGuiConnection(objApplication)
    End If
    
    If (objSession Is Nothing) Then
        Set objSession = GetSAPGuiSession(objConnection)
    End If
    
    With objSession
               
        Dim intItemsShown As Integer
        Dim intVerticalScrollEndPoint As Integer
        Dim intHorizontalScrollEndPoint As Integer
    
        ' Move to the end of the GuiUserArea
        .findById("wnd[0]/usr").HorizontalScrollbar.Position = 10000
        .findById("wnd[0]/usr").VerticalScrollbar.Position = 10000
        
        ' Store end points
        intVerticalScrollEndPoint = .findById("wnd[0]/usr").VerticalScrollbar.Position
        intHorizontalScrollEndPoint = .findById("wnd[0]/usr").HorizontalScrollbar.Position
        
        ' Move to the start of the GuiUserArea
        .findById("wnd[0]/usr").HorizontalScrollbar.Position = 0
        .findById("wnd[0]/usr").VerticalScrollbar.Position = 0
        
        ' Items per page being shown
        intItemsShown = objSession.findById("wnd[0]/usr").Children.Count - 1
        
        Dim i As Integer
        Dim n As Integer
        
        For i = 0 To intVerticalScrollEndPoint Step intItemsShown
            .findById("wnd[0]/usr").VerticalScrollbar.Position = i
            intItemsShown = objSession.findById("wnd[0]/usr").Children.Count - 1
            For n = 0 To intItemsShown
                Debug.Print .findById("wnd[0]/usr").Children.ElementAt(n).Text
            Next n
        Next i
        
    End With
End Sub
              

The code shown above works perfectly except for the following statements:

  • It almost parses all kind of GuiUserArea reports, except for the ones with a wide horizontal window. I am working to fix these issue but there is a lack of documentation for the SAPGUI object.

  • Slow and very slow for a big amount of data (As it is supposed to be, since We are using VBA to COM Objects). Tried to work with .NET and SAPGUI object with no sucess in order to speed up the process.

But at the end of the road, it seems that SAPGUI object was not designed for these kind of tasks.

Questions!

  • Do you have any other method to try to parse GuiUserArea?
  • Have you tried to use a high level programming language (or even a scripting language) in order to interact with SAP system instead of VBA?
  • Do you know if there is any other way to interact with SAP system rather than SAPGUI Object (Have you tried SAP .NET Connector?)

Solution

  • Your code seems buggy and slow for the following reasons:

    • intItemsShown is being set, then you use it as a step within a loop and eventually modify it within the loop.
    • You have a nested loop that just does a Debug.Print which might slow down your app further
    • Since you seem to be traversing a hierarchy of objects, it appears to me that it would be more appropriate to use recursive calls to
      process children, children of children... Parsing SAP output structures might work but is not clean and will probably become a maintenance nightmare in the future.

    My recommended alternatives

    The first option requires ABAP on the SAP side. The two other options assumes that you have a web server integrated with SAP.

    For i = 0 To intVerticalScrollEndPoint Step intItemsShown ' <--intItemsShown is being used here
        .findById("wnd[0]/usr").VerticalScrollbar.Position = i
        intItemsShown = objSession.findById("wnd[0]/usr").Children.Count - 1 ' and modified here
        For n = 0 To intItemsShown ' and used here again
            Debug.Print .findById("wnd[0]/usr").Children.ElementAt(n).Text
        Next n
    Next i