Skip to Content

VBA scripting - is it possible to log on by SID rather than by name

I have a VBA script that logs into SAP, inputs some data from an Excel sheet, pulls some results and copies them back to Excel. The script works fine on my machine, but not on some others, and I have discovered that this is because the "Description" specified in the System Connection Parameters is not the same on every machine (no idea why). To get around this, I'm wondering if it is possible to use the System ID field instead, as that is the same on all machines here:

The relevant portion of the code is here:

If Not IsObject(SAPguiApp) Then
On Error Resume Next
Set SAPguiAuto = GetObject("SAPGUI")
If Err.Number <> 0 Then
On Error GoTo 0
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe")

Do While Not WshShell.AppActivate("SAP Logon 750")
sleep 1
Loop
Set SAPguiAuto = GetObject("SAPGUI")
Else
On Error GoTo 0
End If
Set SAPguiApp = SAPguiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = SAPguiApp.OpenConnection("name string here", True)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If

If session.Children.Count > 1 Then
session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").Select
session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").SetFocus
session.findById("wnd[1]/tbar[0]/btn[0]").press
End If

I have also included the portion that allows multiple sessions as it took me a REALLY long time to figure out how to do that today and it might be helpful to someone.

Add comment
10|10000 characters needed characters exceeded

  • As an aside, related to your comments around detecting multiple children.

    There are methods to attach your script to an existing session, but you can also test for screen elements with something like this arrangement.

    'check for "unexpected" popups i.e. test for the existence of a screen element. Wont crash the script if nothing is there. 
    Do While Not session.findById("wnd[1]", False) Is Nothing
        If Not session.findById("wnd[1]/usr/radMULTI_LOGON_OPT3", False) Is Nothing Then
    		'Decide what to do if there are multiple logins allowed
    		if 1 = 2 then 'some meaningful if statement here, or just use this to arbitrarily decide. 
    			session.findById("wnd[1]/usr/btnBUTTON_2").press
    		else
    			session.findById("wnd[1]/usr/btnBUTTON_3").press
    		end if
        End If
    	'some other test as an example for nested tests for existence. 
        If Not session.findById("wnd[1]/usr/btnSPOP-OPTION2", False) Is Nothing Then
            session.findById("wnd[1]/usr/btnSPOP-OPTION2").press
        End If
    Loop
    <br>

    Just replace the ID's with what you need for various circumstances.

  • Get RSS Feed

2 Answers

  • Feb 04 at 07:12 AM

    Hello Mark,

    my suggestion is to use an alternative. Try the IP address and the system number instead the system description, e.g. like this

        '-------------------------------------------------------------------
    '- /H/ and the IP address of the system
    '- /S/ 3200 + the system number, in this example 2
    '------------------------------------------------------------------- Set Connection = SAPguiApp.OpenConnectionByConnectionString("/H/10.100.200.300/S/3202", True, False)

    The IP address and the system number are always the same. To get the system number call FM GET_SYSTEM_NUMBER with TAC SE37.

    Cheers
    Stefan

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 05 at 11:16 PM

    Hi Stefan,

    Thanks for the suggestion, however, our somewhat restrictive policies seem to mean that I'm not authorised to use t-code SE37. Is there anywhere else from where I could retrieve this value?

    Thanks again!

    Mark

    Add comment
    10|10000 characters needed characters exceeded

    • Sorry for the long delay in replying! Unfortunately, no, I can't access SM51 either! I did look around for the file you mention, it seems most of the config files are stored locally, but I can't find the specific file. However, I think I found a simple work around; I added this to the end of the logon portion of my vb script:

      If session.Children.Count > 1 Then
          session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").Select
          session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").SetFocus
          session.findById("wnd[1]/tbar[0]/btn[0]").press
      End If

      ...and that has taken care of it, now it just simulates the user option of selecting to create another session, which seems becomes the active session by default, so it works OK. The only weakness I can think of here is that if there were already the maximum number of sessions open, it would probably crash, but it's pretty unlikely that someone would leave that many sessions active, so I can live with it.