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

Use the VBA debugger to analyze SAP GUI Scripting objects at runtime

Yesterday I discussed with a friend of mine about different methods of analyzing SAP GUI Scripting programs at runtime. One very easy method, to understand and to see where the attributes or methods are in the SAP GUI Scripting hierarchy, is the Visual Basic for Application (VBA) debugger and its watch window.

You can use VBA from a lot of applications, e.g. Microsoft Office Word or Excel. Press Alt+F11 to open the VBA IDE. At first you must choose the menu item Tools, References and search for the file C:\Program Files\SAP\FrontEnd\SAPgui\sapfewse.ocx, to bind the SAP GUI Scripting API to your VBA program.

Now you can use the following program to check the possibilities:

Option Explicit

Sub Test()

  Dim SapGuiAuto As Object
  Dim Application As SAPFEWSELib.GuiApplication
  Dim Connection As SAPFEWSELib.GuiConnection
  Dim Session As SAPFEWSELib.GuiSession
  Dim Window As SAPFEWSELib.GuiModalWindow
  Dim Coll As SAPFEWSELib.GuiCollection
  
  Set SapGuiAuto = GetObject("SAPGUI")
  If Not IsObject(SapGuiAuto) Then
    Exit Sub
  End If
  
  Set Application = SapGuiAuto.GetScriptingEngine()
  If Not IsObject(Application) Then
    Exit Sub
  End If
  
  Set Connection = Application.Connections(0)
  If Not IsObject(Connection) Then
    Exit Sub
  End If
  
  Set Session = Connection.Sessions(0)
  If Not IsObject(Session) Then
    Exit Sub
  End If
  
  Set Window = Session.FindById("wnd[1]")
  If IsObject(Window) Then
    Set Coll = Window.DumpState("")
    Stop
  End If
  
  Set Coll = Nothing
  Set Window = Nothing
  Set Session = Nothing
  Set Connection = Nothing
  Set Application = Nothing
  Set SapGuiAuto = Nothing

End Sub

 

Execute it in debug mode with F5, it stops at line 35. Choose the variable Coll and add it to your watch window. Open the GuiCollection in the watch window and view the complete hierarchy of the object and the values of the attributes, like in the following picture.

 

/wp-content/uploads/2013/03/neu_1_199718.jpg

 

The VBA debugger offers many possibilities to analyze SAP GUI Scripting objects at runtime.

Assigned Tags

      11 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Stefan,

      Thank you for your examples! As someone who has spent considerable time recently looking into scripting for SAP within VBA, I have found your posts/examples quite helpful.

      I am using SAP NetWeaver BC and when running this code it fails trying to set a session from the Connection variable (I changed "SAPGUI" to "SAPGUISERVER" to get this far based on other information you have posted).

      My understanding is the reason this fails is because server side scripting is disabled, because when viewing local variables, Connection.DisabledByServer = true, but I cannot confirm this to be the root cause. Your utilities Bebo and tracker also do not display information about my SAP session, but I think they have the same problem.

      Do you know what the DisabledByServer attribute represents? My understanding is I need someone with permissions to enable server-side scripting using the transaction RZ11 to fix this and allow scripting to work.

      Best regards,

      Alden

      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Alden,

      thanks for your comment.

      The property DisabledByServer from the GuiConnection object is read only and is set to true if the scripting support has been disabled for the application server. It seems that the scripting support in your case is disabled.

      There are a few parameters to control SAP GUI Scripting by the server:

      • sapgui/user_scripting must be TRUE to use scripting functions
      • sapgui/user_scripting_disable_recording if it is TRUE then it is only possible to run scripts
      • sapgui/user_scripting_force_notification if it is TRUE a notification is always displayed at the frontend
      • sapgui/user_scripting_per_user if it is TRUE you need S_SCR authority to use scripting
      • sapgui/user_scripting_set_readonly if it is TRUE it is not allowed to change the state of SAP GUI

      It is a good idea to look at the profile parameters via RZ11, it seems that scripting is generally allowed but restricted with another parameter. You can check the profile parameters with the report RSPARAM too.

      Bebo does not yet support NWBC for Desktop, but the Scripting Tracker does - I checked it with NWBC Version 4 PL 4.

      Let us know your result.

      Cheers

      Stefan

      Author's profile photo Former Member
      Former Member

      Wonderful! This will be very helpful as well, especially when I begin the "convince SAP administrators to allow client side scripting" process.

      I am fairly inexperienced with using SAP/NWBC but have a significant programming background... but that doesn't explain why I put "false" when I meant "Connection.DisabledByServer = true" though 🙂

      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Alden,

      sorry, I am a little bit confused.

      Connection.DisabledByServer = true means that the Scripting is disabled on your application server. Set sapgui/user_scripting via RZ11 to TRUE and all works well - I hope.

      Let us know your result and good scripting.

      Cheers

      Stefan

      Author's profile photo Former Member
      Former Member

      My apologies. I had incorrectly said the value was "false" - it is actually true.

      I do not have the correct privileges within SAP to change it myself, unfortunately.

      Thank you for all the help!

      Best regards,

      Alden

      Author's profile photo Sayuti Azmi
      Sayuti Azmi

      thanks for this post. triggered an idea on how to solve my issue on getting VBA to respond to sapgui events.

      Author's profile photo Former Member
      Former Member

      Dear Stefan,

      how can i define the SAPFEWSELib ? Actually i get an error using it... thanks so far.

      Author's profile photo Former Member
      Former Member

      Have you done this?

      At first you must choose the menu item Tools, References and search for the file C:\Program Files\SAP\FrontEnd\SAPgui\sapfewse.ocx, to bind the SAP GUI Scripting API to your VBA program.

      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author
      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hallo Maximilian,

      you are using a German version of a VBA-IDE. Choose the menu item Extras > Verweise, I think Tools > References in English. Press the button "Durchsuchen..." - "Browse..." and choose the file C:\Program Files\SAP\FrontEnd\SAPgui\sapfewse.ocx. This binds the SAP GUI Scripting API into the VBA-IDE. Press the Ok button and now you can use the SAPFEWSELib namespace.

      Good luck.

      Cheers

      Stefan

      Author's profile photo huang patrick
      huang patrick

      Very useful skills, thanks.