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