Skip to Content
avatar image
Former Member

VBScript: Details on Connection errors

Hello,

has anyone ever found out if you can catch the proper error when you cant logon to SAP via VBscript due to user issues?

There is the <connection>.isconnected() object, but that only returns 1 for connected and 8 for disconnect.

And also the <connection>.LastError pbject, but I havent been able to get anything out of it at all.

I am really looking to get the information you get in the SAPGUI status bar like "user locked" or "out of date range" etc ...

Thanks,

Andi

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

15 Answers

  • Best Answer
    avatar image
    Former Member
    Feb 07, 2008 at 09:30 AM

    I couldnt test it yet, but this might help you:

    Property messageAsPopup As Boolean (Read only)
    Description: Some messages may be displayed not only on the
    	     statusbar but also as a pop-up window. In such
    	     cases, this property is set to True so that a
    	     script knows it has to close a pop-up to continue.

    More information can be found in the SAP Gui scripting manual.

    Good luck.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hello,

      thanks for the tip, but how would I make this work in VBscript?

      I tried tying it to the connection object but it just throws errors?

      Thanks again,

      Andi

  • avatar image
    Former Member
    Feb 08, 2008 at 09:00 AM

    sorry for the late answer, i guess you should include the SAP Scripting Control (sapfewse.ocx) in Visual Basic

    Object = GuiStatusbar

    btw I also seen code like:

    SAPGuiSession("Session").SAPGuiWindow("Create Standard").

    SAPGuiStatusBar("StatusBar").Output

    I didnt test anything, im just trying to help you =)

    hope this helps

    Good luck

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 15, 2008 at 06:57 PM

    Have you tried...

    if session.findById("wnd[0]/sbar").text=

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hello,

      thanks for all your thoughts, highly appriciated!

      I still dont undersand how to put this into my code though.

      BTW - my scripts work fine and well, retrieving performance data out of ABAP. The idea of this execrcise is to write a proper log file/alert on why the connection did not work.

      Set FunctionCtrl = CreateObject("SAP.Functions")
      Set SapConnection = FunctionCtrl.Connection
      SapConnection.client = "<CLNT>"        
      SapConnection.user = "<USER>" 
      SapConnection.password = "<PASS>" 
      SapConnection.language = "<LANG>"
      SapConnection.HostName = "<HOST>"
      SapConnection.SystemNumber = "<NR>"
      If Not SapConnection.Logon(0, True) Then
      	<Somehow get what the actual error was>   
          else
      	<Excellent, connected, do some useful stuff>
      SapConnection.logoff 
      End If
      

      I am not entirely sure but the examles given above all show a GUI screen, is that right?

      Thanks again,

      Andi

  • avatar image
    Former Member
    Feb 19, 2008 at 08:25 AM

    Jerry Borst, how would you like to use that in VBS if you would like to show a messagebox of the warnings that is shown in the GuiStatusbar of SAPGui? I guess not like that...

    Andreas Appelbaum,

    I know what you mean. I have an Excel file which can download tables into a spreadsheet without opening SAPGui. The beginning of this code is actually the same as you posted in your last reply. But I do get error messages when something is wrong. For example if I enter a wrong password, then a messagebox will popup with the following message: "Name or password is incorrent. Please re-enter". So seems like it does have something like an errorhandler.

    Now, Im also looking for a way to have a messagebox of the messages who are shown by the GuiStatusbar...I hope I will find the answers here as I already searched for this via google but wasnt able to find anything about this subject (except for the Gui scripting manual).

    Edited by: Erkan Kopuz on Feb 19, 2008 9:26 AM

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Erkan,

      I see your point, my situation is that I do not want a message box - noone would be there to see it 😊

      I want to write it into a log file.

      Ta,

      Andi

  • avatar image
    Former Member
    Feb 19, 2008 at 10:00 AM

    Andreas,

    So what would you like to see? The errors logged into a file?

    I found out how to get the messages from the GuiStatusbar and write these into a logfile (or popup with msgbox).

    Here an example on how to get the error messages:

    if session.findById("wnd[0]/sbar").text= "You are not authorized to use Transaction SO99" then
    msgbox "You are not authorized to use Transaction SO99",vbCritical,"Warning: No authorisation"
    

    The problem is you have to do some testing to get the errors you need, so you can fix the code.

    I dont have lots of experience in VBS but I do have lots of experience in VBA (almost the same languages tho) and I know how to create a log file into VBA, but somehow I couldnt get it working in VBS (This might be because I do not have the right tools to compile VBS).

    Example VBA:

    Dim WrongLogFileName
    Dim WrongLogtext
    
    If session.findById("wnd[0]/sbar").Text = "You are not authorized to use Transaction SO99" Then
    WrongObject = "No Authorisation"
    ErrDescription = "You are not authorized to use Transaction SO99"
    
    'for testing purposes
    'MsgBox Err.Description & vbNewLine & vbNewLine & vbNewLine & "Error file has been created...", vbCritical, "Error Number" & Err.Number
    
    WrongLogFileName = "C:\ErrorLog.txt"
    WrongLogtext = Date & " om " & Time & " - " & vbCrLf & "     Object : " & WrongObject & vbCrLf & "     Error number: " & Err.Number & vbCrLf & "     Error description: " & vbCrLf & "     " & ErrDescription & vbCrLf
    
    Open WrongLogFileName For Append As #1
    Print #1, WrongLogtext
    Close #1
    end if
    

    Hope this helps you a bit 😊

    Regards,

    Erkan

    Edited by: Erkan Kopuz on Feb 19, 2008 11:02 AM

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hum,

      I tried that, I am starting to wonder if that actually works with VBScript.

      The error I am getting is

      Object doesn't support this property or method:'SapConnection.findById

      Code:

      Set FunctionCtrl = CreateObject("SAP.Functions")
      Set SapConnection = FunctionCtrl.Connection
      SapConnection.client = "<CLNT>"        
      SapConnection.user = "<WRONG_USER>" '<---------- !
      SapConnection.password = "<PASS>" 
      SapConnection.language = "<LANG>"
      SapConnection.HostName = "<HOST>"
      SapConnection.SystemNumber = "<NR>"
      If Not SapConnection.Logon(0, True) Then
      if SapConnection.findById("wnd[0]/sbar").text= "Name or password is incorrect (repeat logon)" then
      msgbox "Name or password is incorrect (repeat logon)",vbCritical,"Warning: No authorisation"
      end if
          else
      	<Excellent, connected, do some useful stuff>
      SapConnection.logoff 
      End If
      

  • avatar image
    Former Member
    Feb 19, 2008 at 10:31 AM

    Indeed, it is lots of work and you only have to do this once. But somehow it seems like no one did this, because I really looked for days to find any information regarding this issue and I was not able to find this information. And even if I found something, it was not good enough....

    Regards,

    Erkan

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 19, 2008 at 10:50 AM

    Im not known with the way you do the login. I use the following:

    Set R3 = CreateObject("SAP.Functions")
    R3.Connection.System = "denmark"
    R3.Connection.client = "100"
    R3.Connection.user = "SAP*"
    R3.Connection.password = ""
    R3.Connection.language = "EN"
    If R3.Connection.logon(0, False) <> True Then
    

    But this will start the SAPlogon...im not sure which method you want to use?

    I just tried your code, but somehow I cant get it working (even without the lines you have added afterwards)...

    Could you give me the right code for the login, so I can try to help you out with that peace of code? We are quite close I guess =)

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      mhh, thats odd, yours is the same, except that it calls a gui window, mine does not.

      Anyway, if you stick this into a .vbs file and just execute it from a command line it should connect fine

      (change your logon details of cousre). At least it does for me

      Set FunctionCtrl = CreateObject("SAP.Functions")
      Set SapConnection = FunctionCtrl.Connection
      
      SapConnection.client = "100"        
      SapConnection.user = "testuser" 
      SapConnection.password = "secret" 
      SapConnection.language = "EN"
      SapConnection.HostName = "willibald"
      SapConnection.SystemNumber = "00"
      
      If Not SapConnection.Logon(0, True) Then
      	msgbox("Not Connected")   
          else
      	msgbox("Connected")
      SapConnection.logoff 
      End If
      

  • avatar image
    Former Member
    Feb 19, 2008 at 12:08 PM

    Are you sure that works? I do get "not connected" all the time. BUT if I use the following code, it does work (but still with the gui login):

    'Create a function object
    Set functionCtrl = CreateObject("SAP.Functions")
    
    'Connect to R/3
    Set sapConnection = functionCtrl.Connection
    sapConnection.Client = "900"
    sapConnection.user = "****" 'Your user id
    sapConnection.Language = "EN"
    sapConnection.password = "****" ' Your password
    SapConnection.HostName = "****"
    SapConnection.SystemNumber = "01"
    If sapConnection.logon(0, False) Then
    MsgBox "No connection to R/3!"
    End If
    

    And this is actually similar to your code, right? Or am I making a mistake here?

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi,

      yes, it works, using this logon procedure in all my scripts without any issues.

      Have a look in the RFC traces to see why the connection does not establish.

      A.

  • avatar image
    Former Member
    Feb 19, 2008 at 12:55 PM

    I guess I dont have enough priviliges to test that. Somehow I do get "Connection refused" in my log files. Though, I can create a connection or I can connect to an existing connection. I just tested this code and it worked:

    If Not IsObject(application) Then
       Set SapGuiAuto  = GetObject("SAPGUI")
       Set application = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(connection) Then
       Set connection = application.Children(0)
    End If
    If Not IsObject(session) Then
       Set session    = connection.Children(0)
    End If
    If IsObject(WScript) Then
       WScript.ConnectObject session,     "on"
       WScript.ConnectObject application, "on"
    End If
    session.findById("wnd[0]").maximize
    session.findById("wnd[0]/mbar/menu[5]/menu[4]").select
    if session.findById("wnd[0]/sbar").text= "You are not authorized to use Transaction SO99" then
    msgbox "You are not authorized to use Transaction SO99",vbCritical,"Warning: No authorisation"
    
    end if
    

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Apr 08, 2008 at 03:05 PM

    Hi Erkan Kopuz,

    I tried to use the code you add to the blog but I got an error

    Object does not support this property or method at libe

    Set Connection = FunctionCtrl.Children(0)

    Could you please help me to solve this issu, I don´t want to receive SAP error, I need to show my own message error

    Thanks

    SapConnection.client = "160"

    SapConnection.user = "sap*"

    SapConnection.Password = "brs"

    SapConnection.Language = "EN"

    SapConnection.HostName = "cpfl0052"

    SapConnection.SystemNumber = "20"

    If Not IsObject(Application) Then

    Set SapGuiAuto = GetObject("SAPGUI")

    ' Set Application = SapGuiAuto.GetScriptingEngine

    End If

    If Not IsObject(Connection) Then

    Set Connection = FunctionCtrl.Children(0)

    End If

    If Not IsObject(session) Then

    Set session = Connection.Children(0)

    End If

    If IsObject(WScript) Then

    WScript.ConnectObject session, "on"

    WScript.ConnectObject Application, "on"

    End If

    session.findById("wnd[0]").maximize

    session.findById("wnd[0]/mbar/menu[5]/menu[4]").Select

    If session.findById("wnd[0]/sbar").Text = "You are not authorized to use Transaction SO99" Then

    MsgBox "You are not authorized to use Transaction SO99", vbCritical, "Warning: No authorisation"

    End If

    Add comment
    10|10000 characters needed characters exceeded