-1

I have a code that tries to open the SAP GUI application and after putting the user's credentials, it runs a transaction and downloads the .xlsx file on the local.

In the second part of the code which is the exact .vbs script that I recorded in SAP GUI and pasted in Spyder.

I get a syntax error at the line if not IsObject(application) Then, how to solve it?

NB: I tried to use the same indentation as I used in Spyder after several edits.

< Importing the Libraries >


    import win32com.client
    import sys
    import subprocess
    import time
    
    
    ##This function will Login to SAP from the SAP Logon window
    
    def saplogin():
    
        try:
    
            path = r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe"
            subprocess.Popen(path)
            time.sleep(10)
    
            SapGuiAuto = win32com.client.GetObject('SAPGUI')
            if not type(SapGuiAuto) == win32com.client.CDispatch:
                return
    
            application = SapGuiAuto.GetScriptingEngine
            if not type(application) == win32com.client.CDispatch:
                SapGuiAuto = None
                return
            connection = application.OpenConnection("=PR1 [Assembly & Test] router 1", True)
    
            if not type(connection) == win32com.client.CDispatch:
                application = None
                SapGuiAuto = None
                return
    
            session = connection.Children(0)
            if not type(session) == win32com.client.CDispatch:
                connection = None
                application = None
                SapGuiAuto = None
                return
    
            session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "UName"
            session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "Pass"
            session.findById("wnd[0]").sendVKey(0)
    ##This part is the SAP .vbs script which is recorded in the SAP and pasted here as-is: **>
            if not IsObject(application) Then ##here I get the invalid syntax error.

                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]/tbar[0]/okcd").text = "SPT52"
            session.findById("wnd[0]/tbar[0]/btn[0]").press
    
            session.findById("wnd[0]").maximize
            session.findById("wnd[0]/usr/chkP_17REQS").selected = false
            session.findById("wnd[0]/usr/chkP_18REQS").selected = false
            session.findById("wnd[0]/usr/chkP_10REQS").selected = false
            session.findById("wnd[0]/usr/chkP_ENGINE").selected = false
            session.findById("wnd[0]/usr/chkP_DETAIL").selected = false
            session.findById("wnd[0]/usr/radRB_FILE").select
            session.findById("wnd[0]/usr/ctxtP_PLANT").text = "0010"
            session.findById("wnd[0]/usr/ctxtPD_SEL-LOW").text = "86A"
            session.findById("wnd[0]/usr/txtP_FDAY2").text = "0"
            session.findById("wnd[0]/usr/txtP_WEEK2").text = "52"
            session.findById("wnd[0]/usr/txtP_MONTH2").text = "0"
            session.findById("wnd[0]/usr/txtP_FDAY2").setFocus
            session.findById("wnd[0]/usr/txtP_FDAY2").caretPosition = 3
            session.findById("wnd[0]/tbar[1]/btn[8]").press
            session.findById("wnd[1]/usr/ctxtDY_PATH").setFocus
            session.findById("wnd[1]/usr/ctxtDY_PATH").caretPosition = 0
            session.findById("wnd[1]").sendVKey 4
            session.findById("wnd[2]").close
            session.findById("wnd[1]/usr/ctxtDY_PATH").text = "C:\Users\Documents\SAP\SAP GUI\"
            session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "SP032.xls"
            session.findById("wnd[1]/usr/ctxtDY_FILENAME").setFocus
            session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 8
            session.findById("wnd[1]/tbar[0]/btn[11]").press
            session.findById("wnd[1]/tbar[0]/btn[0]").press
            session.findById("wnd[0]/tbar[0]/btn[3]").press
    
        except:
              print(sys.exc_info()[0])
        finally:
              session = None
              connection = None
              application = None
              SapGuiAuto = None
    saplogin()



I did the changes after Sandra Rossi's comment as following but get a new error:

import win32com.client import sys import subprocess import time

This function will Login to SAP from the SAP Logon window

try:

    path = r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe"
    subprocess.Popen(path)
    time.sleep(2)

    SapGuiAuto = win32com.client.GetObject('SAPGUI')
    if not type(SapGuiAuto) == win32com.client.CDispatch:
        return

    application = SapGuiAuto.GetScriptingEngine
    if not type(application) == win32com.client.CDispatch:
        SapGuiAuto = None
        return
    connection = application.OpenConnection("=PR1 [Assembly & Test] router 1", True)

    if not type(connection) == win32com.client.CDispatch:
        application = None
        SapGuiAuto = None
        return

    session = connection.Children(0)
    if not type(session) == win32com.client.CDispatch:
        connection = None
        application = None
        SapGuiAuto = None
        return

    session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "Uname"
    session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "Pass"
    session.findById("wnd[0]").sendVKey(0)

 


    session.findById("wnd[0]").resizeWorkingPane (98,16,False)
    session.findById("wnd[0]/tbar[0]/okcd").text = "SP032"
    session.findById("wnd[0]").sendVKey (0)
    session.findById("wnd[0]/usr/radP_MATL").select
    session.findById("wnd[0]/usr/chkP_LTPC").selected = False
    session.findById("wnd[0]/usr/ctxtS_FEVOR-LOW").text = "86A"
    session.findById("wnd[0]/usr/radP_SORT3").setFocus
    session.findById("wnd[0]/usr/radP_SORT3").select
    session.findById("wnd[0]/tbar[1]/btn[8]").press
    session.findById("wnd[0]/usr").verticalScrollbar.position = 1
    session.findById("wnd[0]/usr").verticalScrollbar.position = 2
    session.findById("wnd[0]/usr").verticalScrollbar.position = 3
    session.findById("wnd[0]/mbar/menu[0]/menu[1]/menu[2]").select
    session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").select
    session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").setFocus
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    session.findById("wnd[1]/usr/ctxtDY_PATH").text = "D:\86A"
    session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "SP032.txt"
    session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 9
    session.findById("wnd[1]/tbar[0]/btn[11]").press
    session.findById("wnd[0]/tbar[0]/btn[3]").press
    session.findById("wnd[0]/tbar[0]/btn[3]").press

saplogin()

IndentationError: unexpected unindent

Note: As I mentioned in my comments I did a lot of modification but after last one still the weird error comes back as **IndentationError: unexpected unindent

5
  • It seems that the first vbscript statement is not recognized in your python program, is it possible to mix python and vbscript? Commented Dec 28, 2019 at 17:45
  • Thanks for the comment. I have seen in a SAP blog that it should be possible. Also the first block of code which opens and goes through SAP GUI has vbs code in it! Any idea as I am really stuck at this point :( Commented Dec 30, 2019 at 15:03
  • I ask the question because I doubt that it's possible to mix them in the same source code without any mark to tell the compiler/runtime what source code parser should be used... My point is valid for any combination of programming languages in the same source code of course... You should generalize your question to execute any VBS code from Python but anyway there are already some questions & answers how to call external command in Python. Commented Dec 31, 2019 at 14:59
  • According to this post: blogs.sap.com/2017/09/19/… I suppose it is possible to use SAP scripts in Python, so I decided to try it myself but the result is not working. After so much research I found out may be here people have got the same experience and could help me and future asks to solve it! So I posted the exact code with the necessary explanations. Commented Dec 31, 2019 at 17:21
  • Thanks. I think that the syntax is very close and so a copy/paste of most of VBS code generated by SAP GUI Scripting is identical in Python (as Stefan Schnell says: "The recorded VBScript code can use almost always unchanged, sometimes it is necessary to set some brackets.") Commented Dec 31, 2019 at 19:19

1 Answer 1

0

Some simple code like method call or attribute initialization has the same syntax in VBScript and Python but the rest is not compatible.

For instance, you have to make these adaptations (taken from Stefan Schnell blog post):

Python (the equivalent VBScript code is commented out):

   #session.findById("wnd[0]").resizeWorkingPane 173, 36, 0
    session.findById("wnd[0]").resizeWorkingPane(173, 36, 0)
   #session.findById("wnd[0]/tbar[0]/okcd").text = "/nse16"
    session.findById("wnd[0]/tbar[0]/okcd").text = "/nse16"
   #session.findById("wnd[0]").sendVKey 0
    session.findById("wnd[0]").sendVKey(0)
   #session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").text = "TADIR"
    session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").text = "TADIR"
   #session.findById("wnd[0]").sendVKey 0
    session.findById("wnd[0]").sendVKey(0)
   #session.findById("wnd[0]/tbar[1]/btn[8]").press
    session.findById("wnd[0]/tbar[1]/btn[8]").press()

Now it seems that the part of code which has not a valid Python syntax is not even needed because you already initialized the variables! These are the following lines to be removed from your script:

        if not IsObject(application) Then ##here I get the invalid syntax error.

            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 

Concerning the indentation issue, there are many questions and answers in StackOverflow.

Sign up to request clarification or add additional context in comments.

7 Comments

I already tried this before but just after your suggestion, gave it another try, please see below the message I got for removing these lines as:
Hi again, thanks! I already tried it before posting my question here but got "IndentationError: unindent does not match any outer indentation level" and "SyntaxError: invalid syntax" error based on the "session.findById... " place! If you can try it yourself on Spyder you will see it for yourself. Let me know if there is any other solution...
I think it's because the code you got from SAP GUI Scripting is not compatible with Python, as Stefan Schnell explained with examples, add some brackets (select->select(), sendVKey 0->sendVKey(0), etc.) It's also very simple to locate which lines have a valid Python syntax and which ones are invalid, so maybe you can post those two groups of lines so that to go further. The issue about the indentation is massively documented in StackOverflow.
I tried again with couple of modifications and indentation adjustments, syntax wise there is no glitch and it seems smooth. When I run it the 1st part of code working until it tries to run a transaction and then I got the error: AttributeError: 'NoneType' object has no attribute 'findById'.
That's a different question, could you open a new one please (remember to make a minimal reproducible example, i.e. don't show the subsequent findById statements because only the first one fails). Moreover, explain which objects can't be instantiated (those if ... is None).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.