#include #include #include #include #include #Region DECLARATION ;~ $error Handling Global $iEventerror = 0 ; to be checked to know if com error occurs. ; Must be reset after handling. $oMyError = ObjEvent("AutoIt.Error", "ErrorCatch") ; Install a custom error handler ;- Global $oSAPROT, $oSapGuiAuto, $oApplication, $oSAPconnection, $oSAPsession Global $sLogPath, $sReturnResultText, $bWriteToLog = True, $bWrite2Console, $SAP_Write_Default_Log = False, $iReturnExtended #EndRegion DECLARATION #comments-start DEMOCODE #Region DEMO CODE --------------- START Local $oSAPROT, $oSapGuiAuto, $oApplication, $oSAPsession ;Login into SAP system and returning the return value/object into $vResult Local $connection = SAPlogIN("USERNAME", "password", "SYS Name", "SAP Logon 770") ;If there is an error the $connection will have value, an integer, ;if all is OK then it will return an object $session = $connection.Children(0) $session.createSession ; Create new session ConsoleWrite("========= LOG OFF ===========" & @CRLF) Sleep(5000) ; - Sleep needed because, in this case, new session has not been created jet!!! SAPlogOFF() #EndRegion DEMO CODE --------------- E N D #comments-end DEMOCODE ;~ SAP Functions Func SAPlogIN($sUser, $sPassword, $sSysName = Default, $sGUIversion = Default) ;-======================================================= ;-# LOGIN INTO SAP # ;-# # ;-# This function will log you into SAP with provided # ;-# usename and password. If another user is logged in # ;-# NEW session will be created for that user. # ;-# If successfull this function will return the # ;-# the $connection object. # ;-# The "hook" for the Session (GUI Windows) must be # ;-# made after this function, for example: -> # ;-# -> ($session = $connection.Children(0)) # ;-# # ;-======================================================= ;-# RETURN CODES # ;-# If OK #SAPconnection object is returned # ;-# 1 - SAPROT Failed, DLL is probably missing # ;-# 2 - SAPGUI not loaded # ;-# 3 - Fail to GetScriptingEngine # ;-# 4 - Failed to reach the server # ;-# 5 - Failed to Login, reason in the log # ;-# (Bad password, User validity date is off, ...) # ;-# 6 - Failed to hook to active session (Login) # ;-# (Unknown error) # ;-======================================================= ;-# $sUser - username for SAP # ;-# $sPassword - password # ;-# $sSysName - Login Name from SAP Logon/Pad # ;-# $sGUIversion - SAP GUI window title and version # ;-# For example 'SAP Logon 770' # ;-# # ;-======================================================= Local $oSAPROT, $oSapGuiAuto, $oSAPconnection, $oSAPsession, $oApplication Local $bUserExist = False If $sSysName = Default Then $sSysName = "OTS" If $sGUIversion = Default Then $sGUIversion = "SAP Logon 770" ;~ WinWait for windows tited "SAP Logon 770", "SAP Logon " + SAP GUI version $iEventerror = 0 $oSAPROT = ObjCreate("SapROTWr.SAPROTWrapper") If Not IsObj($oSAPROT) Then ConsoleWrite("! $oSAPROT: Fail" & @CRLF) WriteToLog($sLogPath, "ERROR - SAPlogIN: $oSAPROT: Fail", Default, 3, "-") Return "1 - $oSAPROT: Fail" Else ConsoleWrite("+ $oSAPROT: OK" & @CRLF) EndIf $oSapGuiAuto = $oSAPROT.GetROTEntry("SAPGUI") If Not IsObj($oSapGuiAuto) Then ConsoleWrite("! $oSapGuiAuto: Failed, starting SAP Login..." & @CRLF) Run("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", "") WinWait($sGUIversion, "", 30) Sleep(3000) $oSapGuiAuto = $oSAPROT.GetROTEntry("SAPGUI") If Not IsObj($oSapGuiAuto) Then ConsoleWrite("$oSapGuiAuto: Fail" & @CRLF) WriteToLog($sLogPath, "ERROR - SAPlogIN: $oSapGuiAuto: Fail", Default, 3, "-") Return "2 - $oSapGuiAuto: Fail" EndIf Else ConsoleWrite("+ $oSapGuiAuto: OK" & @CRLF) EndIf $oApplication = $oSapGuiAuto.GetScriptingEngine() If Not IsObj($oApplication) Then ConsoleWrite("! $oApplication: Fail to GetScriptingEngine" & @CRLF) WriteToLog($sLogPath, "ERROR - SAPlogIN: $oApplication: Fail to GetScriptingEngine", Default, 3, "-") Return "3 - $oApplication: Fail to GetScriptingEngine" Else ConsoleWrite("+ $oApplication: OK" & @CRLF) EndIf ConsoleWrite("> Active Sessions (by USER): " & $oApplication.Connections.Count() & @CRLF) If $oApplication.Connections.Count() = 0 Then ConsoleWrite("+ No Active log-in's, LOGING INTO SAP..." & @CRLF) $oSAPconnection = $oApplication.OpenConnection($sSysName, True) If Not IsObj($oSAPconnection) Then ConsoleWrite("! $oSAPconnection: Failed to reach the server" & @CRLF) WriteToLog($sLogPath, "ERROR - SAPlogIN: $oSAPconnection: Failed to reach the server", Default, 3, "-") Return "4 - $oSAPconnection: Failed to reach the server" EndIf $oSAPsession = $oSAPconnection.children(0) $oSAPsession.findById("wnd[0]/usr/txtRSYST-BNAME").Text = $sUser $oSAPsession.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = $sPassword $oSAPsession.findById("wnd[0]").sendVKey(0) While $oSAPsession.Busy Sleep(100) WEnd If $oSAPsession.info.Transaction <> "SESSION_MANAGER" Then WriteToLog($sLogPath, "ERROR - SAPlogIN: " & $oSAPsession.findById("wnd[0]/sbar").text, Default, 3, "-") ConsoleWrite("! ERROR - SAPlogIN: " & $oSAPsession.findById("wnd[0]/sbar").text & @CRLF) Return "5 - $oSAPsession: Failed to LogIn" EndIf Else For $oSAPconnection In $oApplication.Children If $sUser = $oSAPconnection.Children(0).info.user Then $bUserExist = True ConsoleWrite("> user: " & $oSAPconnection.Children(0).info.user & " - $bUserExist: " & $bUserExist & @CRLF) Next ;$oSAPconnection = $oApplication.Children(0) If $bUserExist = False Then ConsoleWrite("+ User not logged in, LOGING INTO SAP..." & @CRLF) $oSAPconnection = $oApplication.OpenConnection($sSysName, True) $oSAPsession = $oSAPconnection.children(0) $oSAPsession.findById("wnd[0]/usr/txtRSYST-BNAME").Text = $sUser $oSAPsession.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = $sPassword $oSAPsession.findById("wnd[0]").sendVKey(0) While $oSAPsession.Busy Sleep(100) WEnd If $oSAPsession.info.Transaction <> "SESSION_MANAGER" Then WriteToLog($sLogPath, "ERROR - SAPlogIN: " & $oSAPsession.findById("wnd[0]/sbar").text, Default, 3, "-") ConsoleWrite("! ERROR - SAPlogIN: " & $oSAPsession.findById("wnd[0]/sbar").text & @CRLF) EndIf Return "5 - $oSAPsession: Failed to LogIn" Else $oSAPsession = $oSAPconnection.children(0) EndIf EndIf If Not IsObj($oSAPsession) Then ConsoleWrite("! $oSAPsession: Failed to LogIn" & @CRLF) WriteToLog($sLogPath, "ERROR - SAPlogIN: $oSAPsession: Failed to LogIn", Default, 3, "-") Return "6 - $oSAPsession: Failed to LogIn" Else ConsoleWrite("+ $oSAPsession: OK" & @CRLF) ;~ Sleep(500) #Region INFO BLOCK $execLine = @ScriptLineNumber & " INFO Block" ConsoleWrite("- Client : " & $oSAPsession.info.Client & @CRLF) ConsoleWrite("- User : " & $oSAPsession.info.User & @CRLF) ConsoleWrite("- Program : " & $oSAPsession.info.Program & @CRLF) ConsoleWrite("- Transaction: " & $oSAPsession.info.Transaction & @CRLF) ConsoleWrite("- Server : " & $oSAPsession.info.ApplicationServer & @CRLF) ConsoleWrite("- Group : " & $oSAPsession.info.Group & @CRLF) ConsoleWrite("- Msg Server : " & $oSAPsession.info.MessageServer & @CRLF) ConsoleWrite("- Screen Num : " & $oSAPsession.info.ScreenNumber & @CRLF) ConsoleWrite("- Session Num: " & $oSAPsession.info.SessionNumber & @CRLF) ConsoleWrite("- System Name: " & $oSAPsession.info.SystemName & @CRLF) ConsoleWrite("- Session Id : " & $oSAPsession.info.SystemSessionId & @CRLF) ConsoleWrite("> -------------------------------------" & @CRLF) Local $fullSystemName = $oSAPsession.info.SystemName & _ "(" & $oSAPsession.info.SessionNumber & ")" & _ "/" & $oSAPsession.info.Client ConsoleWrite("- Full System Name: " & $fullSystemName & @CRLF) ConsoleWrite("> -------------------------------------" & @CRLF) ConsoleWrite("- Sess Handle: " & HWnd($oSAPsession.findById("wnd[0]").Handle) & @CRLF) ConsoleWrite("> =====================================" & @CRLF) #EndRegion INFO BLOCK While $oSAPsession.busy Sleep(1000) WEnd ;~ Handle additional windows like: First time login today window, click button. $execLine = @ScriptLineNumber & " - If $oSAPsession.Children.count > 1 Then" If $oSAPsession.Children.Count > 1 Then $execLine = @ScriptLineNumber & " - Local $MS_winTitle = $oSAPsession.Children(1).text" Local $MS_winTitle = $oSAPsession.Children(1).text ConsoleWrite("- " & $MS_winTitle & @CRLF) Select Case $MS_winTitle = $fullSystemName & " System Messages" Or $MS_winTitle = "System Messages" ; Info message for the 1st login during the day $execLine = @ScriptLineNumber & " - System Messages" $oSAPsession.findById("wnd[1]/tbar[0]/btn[12]").Press ; Press button OK (Green) Case $MS_winTitle = $fullSystemName & " License Information for Multiple Logons" Or $MS_winTitle = "License Information for Multiple Logons" ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $MS_winTitle = ' & $MS_winTitle & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ; User already logged on $execLine = @ScriptLineNumber & " - License Information for Multiple Logons" $oSAPsession.findById("wnd[1]/usr/radMULTI_LOGON_OPT1").Selected = True $oSAPsession.findById("wnd[1]/tbar[0]/btn[0]").Press ; Press button OK (Green) Case $MS_winTitle = $fullSystemName & " Information" Or $MS_winTitle = "Information" ; Failed password logon attempt info $execLine = @ScriptLineNumber & " - Information" $oSAPsession.findById("wnd[1]/tbar[0]/btn[0]").Press ; Press button OK (Green) EndSelect EndIf EndIf If $iEventerror = 0 Then WriteToLog($sLogPath, "OK - SAPlogIN: SUCCESSFULLY Login to '" & $sSysName & "' with user '" & $sUser & "'", Default, 1, "=") WriteToLog($sLogPath, "Client : " & $oSAPsession.info.Client, Default) WriteToLog($sLogPath, "Server : " & $oSAPsession.info.ApplicationServer, Default) WriteToLog($sLogPath, "Group : " & $oSAPsession.info.Group, Default) WriteToLog($sLogPath, "Msg Server : " & $oSAPsession.info.MessageServer, Default) WriteToLog($sLogPath, "System Name: " & $oSAPsession.info.SystemName, Default) WriteToLog($sLogPath, "Full System Name: " & $fullSystemName, Default, 2, "-") WriteToLog($sLogPath, "Sess Handle: " & HWnd($oSAPsession.findById("wnd[0]").Handle), Default, 2, "=") Return $oSAPconnection Else WriteToLog($sLogPath, "ERROR - SAPlogIN: UNKNOW Error Occured. Error: '" & $oMyError.windescription, Default, 3, "-") Return "5 - " & $oMyError.windescription EndIf EndFunc ;==>SAPlogIN Func SAPlogOFF($oSAPconnection = Default) ;-======================================================= ;-# LOGIN OFF the SAP # ;-# # ;-# This function will log you off the SAP. # ;-# # ;-# If $oSAPconnection is used then only that session # ;-# will be logged off. # ;-# # ;-# If the $oSAPconnection has NOT been proveded then # ;-# ALL SESSIONS will be loged off the SAP. # ;-# # ;-======================================================= ;-# RETURN CODES # ;-# If OK #SAPsession object is returned # ;-# 1 - SAPROT Failed, DLL is probably missing # ;-# 2 - SAPGUI not loaded # ;-# 3 - Fail to GetScriptingEngine # ;-# 5 - Error, please check the discription # ;-# # ;-======================================================= Local $oSAPROT, $oSapGuiAuto, $oApplication, $oSAPsession $iEventerror = 0 If $oSAPconnection = Default Then ConsoleWrite("> $oSAPsession = Default" & @CRLF) $oSAPROT = ObjCreate("SapROTWr.SAPROTWrapper") If Not IsObj($oSAPROT) Then Return "1 - $oSAPROT: Fail SapROTWr.SAPROTWrapper" EndIf $oSapGuiAuto = $oSAPROT.GetROTEntry("SAPGUI") If Not IsObj($oSapGuiAuto) Then Return "2 - $oSapGuiAuto: Fail $oSAPROT.GetROTEntry('SAPGUI')" EndIf $oApplication = $oSapGuiAuto.GetScriptingEngine() If Not IsObj($oApplication) Then Return "3 - $oSAPROT: Fail $oSapGuiAuto.GetScriptingEngine()" EndIf For $oSAPconnection In $oApplication.Children If Not $oSAPconnection.DisabledByServer Then For $oSAPsession In $oSAPconnection.Children $oSAPsession.findById("wnd[0]").Close() $oSAPsession.findById("wnd[1]/usr/btnSPOP-OPTION1").press() Next EndIf Next Else For $oSAPsession In $oSAPconnection.Children $oSAPsession.findById("wnd[0]").Close() $oSAPsession.findById("wnd[1]/usr/btnSPOP-OPTION1").press() Next EndIf If $iEventerror = 0 Then Return 0 Else Return "5 - " & $oMyError.windescription EndIf EndFunc ;==>SAPlogOFF Func SAPtable2file($oSAPsession, $tableName, $destFolder, $destFileName = Default, $outFormat = 2) ;-======================================================= ;-# EXPORT SAP TABLE TO FILE # ;-# # ;-# This function will export table from SE16 t-code # ;-# into a TXT file. # ;-# # ;-======================================================= ;-# RETURN CODES # ;-# 0 - OK # ;-# 1 - SAPROT Failed, DLL is probably missing # ;-# 2 - SAPGUI not loaded # ;-# 3 - Fail to GetScriptingEngine # ;-# 4 - Failed to hook to active session (Login) # ;-# 5 - Failed to open transaction SE16 # ;-# 6 - Failed to write file (1 step) # ;-# 10- Unknown error # ;-======================================================= ;-# $tableName - SAP Table Name # ;-# $destFolder - Destination for the export file # ;-# $destFileName - file to export data to # ;-# $outFormat - Export format: # ;-# 1 - Unconverted # ;-# 2 - TAB # ;-# # ;-======================================================= Local $oSAPROT, $oSapGuiAuto, $oApplication, $oSAPconnection ConsoleWrite("> $tableName: " & $tableName & @CRLF) ConsoleWrite("> $destFolder: " & $destFolder & @CRLF) ConsoleWrite("> $destFileName: " & $destFileName & @CRLF) ConsoleWrite("> $outFormat: " & $outFormat & @CRLF) If $destFolder = Default Then $destFileName = @ScriptDir & "\" If $destFileName = Default Then $destFileName = $tableName & ".txt" $iEventerror = 0 $oSAPsession.StartTransaction("SE16") If $iEventerror = 1 Then ConsoleWrite("! 5 - Failed to start transaction SE16" & @CRLF) Return "5" Else ConsoleWrite("Starting of the transaction SE16 Successfull" & @CRLF) EndIf $oSAPsession.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").text = $tableName $oSAPsession.findById("wnd[0]").sendVKey(0) $oSAPsession.findById("wnd[0]/tbar[1]/btn[31]").press $rowCount = $oSAPsession.findById("wnd[1]/usr/txtG_DBCOUNT").text ConsoleWrite("Table: " & $tableName & " | RowCount: " & $rowCount) If $rowCount > 0 Then $oSAPsession.findById("wnd[1]/tbar[0]/btn[0]").press ;~ Select FieldName instead of Field Name $oSAPsession.findById("wnd[0]/mbar/menu[3]/menu[1]").Select $oSAPsession.findById("wnd[1]/usr/tabsG_TABSTRIP/tabp0400/ssubTOOLAREA:SAPLWB_CUSTOMIZING:0400/radSEUCUSTOM-FIELDNAME").Select $oSAPsession.findById("wnd[1]/usr/tabsG_TABSTRIP/tabp0400/ssubTOOLAREA:SAPLWB_CUSTOMIZING:0400/radSEUCUSTOM-FIELDNAME").setFocus $oSAPsession.findById("wnd[1]/tbar[0]/btn[0]").press ;~ GET Max Width $oSAPsession.findById("wnd[0]/usr/ctxtLIST_BRE").setFocus $oSAPsession.findById("wnd[0]/usr/ctxtLIST_BRE").caretPosition = 1 $oSAPsession.findById("wnd[0]").sendVKey(4) $rowLen = $oSAPsession.findById("wnd[1]/usr/sub/1[0,0]/sub/1/3[0,1]/sub/1/3/4[0,4]/lbl[12,4]").text $oSAPsession.findById("wnd[1]/tbar[0]/btn[0]").press $oSAPsession.findById("wnd[0]/usr/ctxtLIST_BRE").text = $rowLen $oSAPsession.findById("wnd[0]/usr/txtMAX_SEL").text = $rowCount $timer1 = TimerInit() $oSAPsession.findById("wnd[0]/tbar[1]/btn[8]").press $oSAPsession.findById("wnd[0]/mbar/menu[1]/menu[5]").Select Switch $outFormat Case "1" ;~ UNCONVERTED $oSAPsession.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").Select $oSAPsession.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").setFocus Case "2" ;~ Text with TABs $oSAPsession.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").Select $oSAPsession.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").setFocus EndSwitch ConsoleWrite("Table: " & $tableName & " | RowCount: " & $rowCount & " | EXPORTING, Please Wait." & @CRLF) $oSAPsession.findById("wnd[1]/tbar[0]/btn[0]").press $oSAPsession.findById("wnd[1]/usr/ctxtDY_PATH").text = $destFolder $oSAPsession.findById("wnd[1]/usr/ctxtDY_FILENAME").text = $destFileName If FileExists($destFolder & $destFileName) Then $oSAPsession.findById("wnd[1]/tbar[0]/btn[11]").press Else $oSAPsession.findById("wnd[1]/tbar[0]/btn[0]").press EndIf ;~ $oSAPsession.findById("wnd[0]/tbar[0]/btn[15]").press ;~ $oSAPsession.findById("wnd[0]/tbar[0]/btn[15]").press EndIf ConsoleWrite("| Duration: " & _msToTime(TimerDiff($timer1), 0) & @CRLF) FileWriteLine($destFolder & "ExportTable.Log", " | " & _NowCalc() & " | Table: " & $tableName & _ " | RowCount: " & $rowCount & " | Duration: " & _msToTime(TimerDiff($timer1))) $timer1 = Null If StringInStr($oSAPsession.findById("wnd[0]/sbar").text, "Download") > 0 And _ StringInStr($oSAPsession.findById("wnd[0]/sbar").text, $destFolder & $destFileName) > 0 Then Return 0 Else Return "6" EndIf Return "10" EndFunc ;==>SAPtable2file Func SAPtable2array($oSAPsession, $tableName) ;-==================================================== ;- RETURN CODES ;- 0 - OK ;- 1 - SAPROT Failed, DLL is probably missing ;- 2 - SAPGUI not loaded ;- 3 - Fail to GetScriptingEngine ;- 4 - Failed to hook to active session (Login) ;- 5 - Failed to open transaction SE16 ;- 6 - Failed to write file (1 step) ;-==================================================== ;- $tableName - SAP Table Name ;- This function will return an Array ;- Local $oSAPROT, $oSapGuiAuto, $oApplication, $oSAPconnection $iEventerror = 0 Local $destFolder = @ScriptDir & "\" Local $destFileName = "tempTable.tmp" Local $inFile, $outFile, $limit, $progres, $endOfFile, $line, $n, $tempArray Local $pd = 2 #Region EXPORT Table $iEventerror = 0 $oSAPsession.StartTransaction("SE16") If $iEventerror = 1 Then ConsoleWrite("5 - Failed to start transaction SE16" & @CRLF) Return "5" Else ConsoleWrite("Starting of the transaction SE16 Successfull" & @CRLF) EndIf $oSAPsession.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").text = $tableName $oSAPsession.findById("wnd[0]").sendVKey(0) $oSAPsession.findById("wnd[0]/tbar[1]/btn[31]").press $rowCount = $oSAPsession.findById("wnd[1]/usr/txtG_DBCOUNT").text ConsoleWrite("Table: " & $tableName & " | RowCount: " & $rowCount) If $rowCount > 0 Then $oSAPsession.findById("wnd[1]/tbar[0]/btn[0]").press ;~ Select FieldName instead of Field Name $oSAPsession.findById("wnd[0]/mbar/menu[3]/menu[1]").Select $oSAPsession.findById("wnd[1]/usr/tabsG_TABSTRIP/tabp0400/ssubTOOLAREA:SAPLWB_CUSTOMIZING:0400/radSEUCUSTOM-FIELDNAME").Select $oSAPsession.findById("wnd[1]/usr/tabsG_TABSTRIP/tabp0400/ssubTOOLAREA:SAPLWB_CUSTOMIZING:0400/radSEUCUSTOM-FIELDNAME").setFocus $oSAPsession.findById("wnd[1]/tbar[0]/btn[0]").press ;~ GET Max Width $oSAPsession.findById("wnd[0]/usr/ctxtLIST_BRE").setFocus $oSAPsession.findById("wnd[0]/usr/ctxtLIST_BRE").caretPosition = 1 $oSAPsession.findById("wnd[0]").sendVKey(4) $rowLen = $oSAPsession.findById("wnd[1]/usr/sub/1[0,0]/sub/1/3[0,1]/sub/1/3/4[0,4]/lbl[12,4]").text $oSAPsession.findById("wnd[1]/tbar[0]/btn[0]").press $oSAPsession.findById("wnd[0]/usr/ctxtLIST_BRE").text = $rowLen $oSAPsession.findById("wnd[0]/usr/txtMAX_SEL").text = $rowCount $timer1 = TimerInit() $oSAPsession.findById("wnd[0]/tbar[1]/btn[8]").press $oSAPsession.findById("wnd[0]/mbar/menu[1]/menu[5]").Select ;~ Text with TABs $oSAPsession.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").Select $oSAPsession.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").setFocus ConsoleWrite("Table: " & $tableName & " | RowCount: " & $rowCount & " | EXPORTING, Please Wait." & @CRLF) $oSAPsession.findById("wnd[1]/tbar[0]/btn[0]").press $oSAPsession.findById("wnd[1]/usr/ctxtDY_PATH").text = $destFolder $oSAPsession.findById("wnd[1]/usr/ctxtDY_FILENAME").text = $destFileName If FileExists($destFolder & $destFileName) Then $oSAPsession.findById("wnd[1]/tbar[0]/btn[11]").press Else $oSAPsession.findById("wnd[1]/tbar[0]/btn[0]").press EndIf ;- $oSAPsession.findById("wnd[0]/tbar[0]/btn[15]").press ;- $oSAPsession.findById("wnd[0]/tbar[0]/btn[15]").press EndIf ConsoleWrite(" | Duration: " & _msToTime(TimerDiff($timer1), 0) & @CRLF) ;FileWriteLine(_NowCalc & " - " & $destFolder & "ExportTable.Log", " | Table: " & $tableName & _ ; " | RowCount: " & $rowCount & " | Duration: " & _msToTime(TimerDiff($timer1))) $timer1 = Null If StringInStr($oSAPsession.findById("wnd[0]/sbar").text, "Download") > 0 And _ StringInStr($oSAPsession.findById("wnd[0]/sbar").text, $destFolder & $destFileName) > 0 Then Else Return "6" EndIf #EndRegion EXPORT Table #Region FIX Table $inFile = FileOpen($destFolder & $destFileName) $outFile = FileOpen($destFolder & "tempFixed.tmp", 10) $limit = _FileCountLines($destFolder & $destFileName) ConsoleWrite("! Total rows in " & $destFileName & ": " & $limit & @CRLF) $e = 1 Do $line = FileReadLine($inFile) $endOfFile = @error If $progres <> Floor($e / $limit * 100) Then $progres = Floor($e / $limit * 100) ConsoleWrite("> Fixing " & $destFileName & ": " & _StringRepeat(" ", 3 - StringLen($progres)) & $progres & "% - |" & _StringRepeat("#", Floor($progres / $pd)) & _StringRepeat(" ", 100 / $pd - Floor($progres / $pd)) & "|" & @CRLF) EndIf If StringInStr($line, @TAB) = 1 Then $line = StringTrimLeft($line, 1) ;~ $line = StringTrimRight($line, 1) ;~ $line = StringRegExpReplace($line, "\s*([|])\s*", '","') ;~ $line = StringRegExpReplace($line, "\s*([|])\s*", @TAB) ;~ $line = '"' & $line & '"' FileWriteLine($outFile, $line) EndIf $e = $e + 1 Until $endOfFile #EndRegion FIX Table #Region Table to Array $inFile = StringReplace(FileRead($destFolder & "tempFixed.tmp"), @CRLF, @CR) If FileExists($destFolder & "tempTable.tmp") Then FileDelete($destFolder & "tempTable.tmp") If FileExists($destFolder & "tempFixed.tmp") Then FileDelete($destFolder & "tempFixed.tmp") $tempArray = _MS_StringToMultiDimArray($inFile, @CR, @TAB) For $n = UBound($tempArray, 2) - 1 To 0 Step -1 If $tempArray[0][$n] = "" Then _ArrayColDelete($tempArray, $n) EndIf Next Return $tempArray #EndRegion Table to Array EndFunc ;==>SAPtable2array Func SAP_StartTransaction($oTempSession, $tCode, $timeOut = 10, $sWinWaitTitle = "") ;-======================================================= ;-# START NEW TRANSACTION # ;-# # ;-# This function will START NEW transaction in # ;-# provided ($oTempSession) SAP session. # ;-# # ;-======================================================= ;-# $oTempSession- SAP session to send $tCode to # ;-# $tCode - SAP Transaction Code # ;-# $timeOut - Time to wait for transaction to open # ;-# in seconds. Default is 10 seconds # ;-# # ;-======================================================= ;-# RETURN CODES # ;-# 0 - FAILED to start SAP transaction # ;-# 1 - SAP transaction started successfully # ;-# # ;~# @error values if Reurn code is 0 # ;-# -1 - Failed to open transaction, timed out (10 sec) # ;-# -2 - #tempSession is not an object # ;-# -3 - Bad tCode or tCode = '' or Null # ;-# -4 - You are not authorized to use transaction # ;-# # ;-======================================================= If Not IsObj($oTempSession) Then SetError(-2, -2, 0) Return 0 EndIf If $tCode = "" Or $tCode = Null Then WriteToLog($sLogPath, "ERROR - TCode: :" & $tCode & "' must not be emty!", Default, 3, "-") SetError(-3, -3, 0) Return 0 EndIf $iEventerror = 0 $oTempSession.startTransaction($tCode) Local $startTime = _NowCalc() Local $error = 0, $condition = True While $condition If $oTempSession.info.Transaction = $tCode Then If $sWinWaitTitle <> "" Then If WinExists($sWinWaitTitle) Then WriteToLog($sLogPath, "OK - TCode: '" & $tCode & "', WinTitle used, titles do match.", Default, 2, "-") $condition = False $error = 0 Else WriteToLog($sLogPath, "WARNING - TCode: '" & $tCode & "', WinTitle used, titles do NOT match!", Default, 3, "-") $condition = False $error = 1 EndIf Else WriteToLog($sLogPath, "OK - TCode: '" & $tCode & "', Transaction opened SUCCESSFULLY.", Default, 2, "-") $condition = False $error = 0 EndIf ElseIf _DateDiff('s', $startTime, _NowCalc()) >= 10 Then WriteToLog($sLogPath, "ERROR - TCode: '" & $tCode & "', TimeOut occures (" & $timeOut & ")!", Default, 3, "-") $condition = False $error = -1 ElseIf $iEventerror = 1 Then WriteToLog($sLogPath, "ERROR - TCode: '" & $tCode & "', Exception occured!" & @CRLF & _ _StringRepeat(" ", 29) & "- Windescription is: " & $oMyError.windescription & @CRLF, Default, 3, "-") $condition = False $error = -3 ElseIf $oTempSession.findById("wnd[0]/sbar").Text = "T-Code " & $tCode & " don't exist" Then WriteToLog($sLogPath, "ERROR - TCode: '" & $tCode & "', Transaction do NOT exist!", Default, 3, "-") $sReturnResultText = $oTempSession.findById("wnd[0]/sbar").Text $condition = False $error = -3 ElseIf $oTempSession.findById("wnd[0]/sbar").Text = "You are not authorized to use transaction " & $tCode Then WriteToLog($sLogPath, "ERROR - TCode: '" & $tCode & "', You are NOT authorized to use transaction!", Default, 3, "-") $sReturnResultText = $oTempSession.findById("wnd[0]/sbar").Text $condition = False $error = -4 EndIf Sleep(100) WEnd If $error = 0 Then $iReturnExtended = 0 SetError(0, 0, 0) Return 1 ElseIf $error = 1 Then $iReturnExtended = 1 SetError(1, 1, 1) Return 1 ElseIf $error = -1 Then $iReturnExtended = -1 SetError(-1, -1, 0) Return 0 ElseIf $error = -2 Then $iReturnExtended = -2 SetError(-2, -2, 0) Return 0 ElseIf $error = -3 Then $iReturnExtended = -3 SetError(-3, -3, 0) Return 0 ElseIf $error = -4 Then $iReturnExtended = -4 SetError(-4, -4, 0) Return 0 EndIf EndFunc ;==>SAP_StartTransaction Func SAP_ElementInteraction($oTempSession, $uiElement, $interaction, $sValue = "", $iWait = 10, $hWindow = "", $sWinStartTitle = "", $sWinWaitTitle = "") ;-======================================================= ;-# START NEW TRANSACTION # ;-# # ;-# This function will interact with an element in # ;-# SAP session ($oTempSession). # ;-# # ;-#=====================================================# ;-# $oTempSession... - SAP Session to work on # ;-# # ;-# $uiElement......- PATH of the element to work with # ;-# # ;-# $interaction....- select - radio button selection # ;-# - click/press - button interaction # ;-# - text/set - set the value of the # ;-# element, TextBox for example # ;-# # ;-# $value..........- In case of "text" interaction, # ;-# value to be set in the TextBox # ;-# # ;-# In some cases it is requiered to # ;-# use a delimiter "|" fo some # ;-# interactions, 2 params requiered: # ;~# # ;~# - pressToolbarButton # ;~# - pressToolbarContextButton # ;~# - selectContextMenuItem # ;-# # ;-# $iWait..........- Wait period in secconds # ;-# $hWindow........- Window handle # ;-# $sWinTitle......- Window Title # ;-# # ;-#=====================================================# ;-# RETURN CODES # ;-# 0 - FAILED # ;-# 1 - SUCCESS # ;-# # ;~# @extended values if Reurn code is 0 # ;-# -1 - Wrong interaction value # ;-# -2 - #tempSession is not an object # ;-# -3 - Bad $uiElement or $uiElement = '' or Null # ;-# -4 - Value could not be set, $interaction text # ;-# -5 - Value could not be read, $interaction get/read # ;-# # ;-#=====================================================# Local $bWrite2ConsoleL = False ; <= Local variable Local $oElement, $sTemp, $sTemp2, $oldTime, $aParams Local $iDebugLevel = 0 ;~ Debug for a specific element!!! ;~ If $uiElement = "wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/chkGS_SELFIELDS-MARK[5,2]" And $iDebugLevel = 0 Then ;~ $iDebugLevel = 1 ;~ Else ;~ $iDebugLevel = 0 ;~ EndIf If $iWait = Default Or $iWait = "" Or $iWait < 0 Then $iWait = 10 ;~ If $iWait = 'max' Then $iWait = 9999 If StringInStr($sValue, "|") > 0 Then $aParams = StringSplit($uiElement, "|", 2) #Region DEBUG Params Block If $iDebugLevel >= 1 Then ConsoleWrite('=========================================' & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $uiElement = ' & $uiElement & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('-----------------------------------------' & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $oTempSession = ' & $oTempSession & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $uiElement = ' & $uiElement & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $interaction = ' & $interaction & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sValue = ' & $sValue & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iWait = ' & $iWait & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hWindow = ' & $hWindow & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sWinStartTitle = ' & $sWinStartTitle & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sWinWaitTitle = ' & $sWinWaitTitle & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('-----------------------------------------' & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $bWriteToLog = ' & $bWriteToLog & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $bWrite2Console = ' & $bWrite2Console & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('-----------------------------------------' & @CRLF) #EndRegion DEBUG Params Block If Not IsObj($oTempSession) Then SetError(-2, -2, 0) Return 0 Else $oldTime = _NowCalc() $startTime = _NowCalc() While $oTempSession.Busy If _DateDiff("s", $startTime, _NowCalc()) >= $iWait Then ExitLoop If _DateDiff("s", $oldTime, _NowCalc()) >= 1 Then ConsoleWrite("> Session busy. Waiting " & $iWait - Int(_DateDiff("s", $startTime, _NowCalc())) & " seconds..." & @CRLF) $oldTime = _NowCalc() EndIf Sleep(100) WEnd EndIf If $uiElement = "" Or $uiElement = Null Then SetError(-3, -3, -3) Return 0 EndIf Local $elementText = "" Local $parent = StringLeft($uiElement, StringInStr($uiElement, "/", 0, -1) - 1) If $hWindow = "" Then $hWindow = HWnd($oTempSession.findById(StringLeft($uiElement, StringInStr($uiElement, "/", 0, 1))).Handle) #Region DEBUG GetHAndle Block If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : HWnd (Handle) : ' & $hWindow & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : WinTitle : ' & WinGetTitle($hWindow) & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : WinActivate : ' & WinActivate($hWindow) & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : WinActive : ' & WinActive($hWindow) & @CRLF) If $iDebugLevel > 1 Then ConsoleWrite('-----------------------------------------' & @CRLF) If $iDebugLevel > 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : Text : ' & @CRLF & WinGetText($hWindow) & @CRLF) If $iDebugLevel >= 1 Then ConsoleWrite('=========================================' & @CRLF & @CRLF) #EndRegion DEBUG Params Block EndIf $startTime = _NowCalc() While RecursiveCheck($oTempSession.findById($parent), $uiElement) <> 1 If _DateDiff("s", $startTime, _NowCalc()) >= $iWait Then ExitLoop ConsoleWrite("> Element not fonud, retrying " & $iWait - Int(_DateDiff("s", $startTime, _NowCalc())) & " seconds..." & @CRLF) Sleep(1000) WEnd If RecursiveCheck($oTempSession.findById($parent), $uiElement) <> 1 Then $oTempSession.findbyId($parent).Visualize(True) WriteToLog($sLogPath, "ERROR - Element (" & $uiElement & ") could NOT been found!") If $bWrite2ConsoleL Then ConsoleWrite("! ERROR - Element (" & $uiElement & ") could NOT been found!" & @CRLF) SetError(-3, -3, -3) Return 0 Else $oElement = $oTempSession.findById($uiElement) ;~ WriteToLog($sLogPath, "Element (" & $uiElement & ") has been found!") ;~ $oElement.Visualize(True) If $oElement.DefaultTooltip = "" Then $elementText = $oElement.Tooltip Else $elementText = $oElement.DefaultTooltip EndIf EndIf If $oElement.Changeable <> 1 And ($interaction <> "get" And $interaction <> "read") Then ;~ $oElement.Visualize(True) WriteToLog($sLogPath, "ERROR - Object """ & $elementText & """ DISABLED (" & $uiElement & ") - FAILED") If $bWrite2ConsoleL Then ConsoleWrite("ERROR - Object """ & $elementText & """ DISABLED (" & $uiElement & ") - FAILED" & @CRLF) SetError(-5, -5, -5) Return 0 EndIf ;~ If $interaction = "get" Or $interaction = "read" Or $interaction = "text" Or $interaction = "set" Then If $sValue <> "" Then ;And $sValue <> -1 And $sValue <> True and $sValue <> False Then $sTemp = $elementText & """, Type: " & $oElement.Type & " (" & $oElement.TypeAsNumber & "), Path: " & $uiElement & ", Text: """ & $oElement.text & """" Else $sTemp = $elementText & """, Type: " & $oElement.Type & " (" & $oElement.TypeAsNumber & "), Path: " & $uiElement EndIf If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $oElement.Id = ' & $oElement.Id & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $oElement.setFocus ; Without focus press will not work in 100% cases ?!? Don't ask why, I had 1 failed case! Select Case $interaction = "select" If $sValue = "" Then $oElement.Select If $oElement.selected = 1 Then WriteToLog($sLogPath, "OK - """ & $interaction & """ interaction on element """ & $sTemp) If $bWrite2ConsoleL Then ConsoleWrite("OK - """ & $interaction & """ interaction on element """ & $sTemp) SetError(0, 0, 0) Return 1 Else $oElement.Visualize(True) If $bWrite2ConsoleL Then ConsoleWrite("ERROR - """ & $interaction & """ interaction on element """ & $sTemp & " - FAILED" & @CRLF) WriteToLog($sLogPath, "ERROR - """ & $interaction & """ interaction on element """ & $sTemp & " - FAILED") Return 0 EndIf Else $oElement.Selected = $sValue If $sValue = "True" Then If $oElement.selected = 1 Then WriteToLog($sLogPath, "OK - """ & $interaction & """ interaction, with value: " & $sValue & " ,on element """ & $sTemp) If $bWrite2ConsoleL Then ConsoleWrite("OK - """ & $interaction & """ interaction on element """ & $sTemp) SetError(0, 0, 0) Return 1 Else WriteToLog($sLogPath, "ERROR - """ & $interaction & """ interaction, with value: " & $sValue & " ,on element """ & $sTemp & " - FAILED") If $bWrite2ConsoleL Then ConsoleWrite("OK - """ & $interaction & """ interaction on element """ & $sTemp) ;~ SetError(0, 0, 0) Return 0 EndIf ElseIf $sValue = "False" Then If $oElement.selected = 0 Then WriteToLog($sLogPath, "OK - """ & $interaction & """ interaction, with value: " & $sValue & " ,on element """ & $sTemp) If $bWrite2ConsoleL Then ConsoleWrite("OK - """ & $interaction & """ interaction on element """ & $sTemp) SetError(0, 0, 0) Return 1 Else WriteToLog($sLogPath, "ERROR - """ & $interaction & """ interaction, with value: " & $sValue & " ,on element """ & $sTemp & " - FAILED") If $bWrite2ConsoleL Then ConsoleWrite("OK - """ & $interaction & """ interaction on element """ & $sTemp) ;~ SetError(0, 0, 0) Return 0 EndIf Else WriteToLog($sLogPath, "ERROR - """ & $interaction & """ interaction, with value: " & $sValue & " ,on element """ & $sTemp & " - NOT POSSIBLE") ;~ SetError(0, 0, 0) Return 0 EndIf $oElement.Vizualize(True) EndIf Case $interaction = "click" Or $interaction = "press" $iEventerror = 0 $oElement.press If $iEventerror = 0 Then If $sWinWaitTitle <> "" Then $startTime = _NowCalc() While 1 If $iDebugLevel >= 1 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : _NowCalc() = ' & _NowCalc() & @CRLF) If _DateDiff("s", $startTime, _NowCalc()) >= $iWait Or WinExists($sWinWaitTitle) = 1 Then ExitLoop Sleep(100) WEnd If _DateDiff("s", $startTime, _NowCalc()) >= $iWait Then _ WriteToLog($sLogPath, "ERROR - WinExists(""" & $sWinWaitTitle & """) TIMED-OUT!") EndIf If $bWrite2ConsoleL Then ConsoleWrite("OK - """ & $interaction & """ interaction on element """ & $sTemp & @CRLF) WriteToLog($sLogPath, "OK - """ & $interaction & """ interaction on element """ & $sTemp) SetError(0, 0, 0) Return 1 Else $oElement.Visualize(True) If $bWrite2ConsoleL Then ConsoleWrite("ERROR - """ & $interaction & """ interaction on element """ & $elementText & """ (" & $uiElement & ") FAILED" & @CRLF) WriteToLog($sLogPath, "ERROR - """ & $interaction & """ interaction on element """ & $sTemp & """ FAILED") Return 0 EndIf Case $interaction = "text" Or $interaction = "set" If $oElement.TypeAsNumber = 34 Then If $oElement.key = $sValue Then If $bWrite2ConsoleL Then ConsoleWrite("OK - """ & $interaction & """ interaction on element """ & _ $sTemp & ") = " & $sValue & " (already set)" & @CRLF) WriteToLog($sLogPath, "OK - """ & $interaction & """ interaction on element """ & $sTemp & " = " & $sValue & " (already set)") SetError(0, 0, 0) Return 1 Else $oElement.key = $sValue If $oElement.key = $sValue Then If $bWrite2ConsoleL Then ConsoleWrite("OK - """ & $interaction & """ interaction on element """ & $sTemp & " = " & $sValue & @CRLF) WriteToLog($sLogPath, "OK - """ & $interaction & """ interaction on element """ & $sTemp & " = " & $sValue & """") SetError(0, 0, 0) Return 1 Else $oElement.Visualize(True) SetError(-4, -4, -4) WriteToLog($sLogPath, "ERROR - """ & $interaction & """ interaction on element """ & $sTemp & " = " & $sValue & """ FAILED") Return 0 EndIf EndIf Else If $oElement.text = $sValue Then If $bWrite2ConsoleL Then ConsoleWrite("OK - """ & $interaction & """ interaction on element """ & _ $sTemp & ") = " & $sValue & " (already set)" & @CRLF) WriteToLog($sLogPath, "OK - """ & $interaction & """ interaction on element """ & $sTemp & ", New Text: """ & $sValue & """ (already set)") SetError(0, 0, 0) Return 1 Else $oElement.text = $sValue If $oElement.text = $sValue Then If $bWrite2ConsoleL Then ConsoleWrite("OK - """ & $interaction & """ interaction on element """ & $sTemp & " = " & $sValue & @CRLF) WriteToLog($sLogPath, "OK - """ & $interaction & """ interaction on element """ & $sTemp & ", New Text: """ & $sValue & """") SetError(0, 0, 0) Return 1 Else $oElement.Visualize(True) SetError(-4, -4, -4) WriteToLog($sLogPath, "ERROR - """ & $interaction & """ interaction on element """ & $sTemp & " = """ & $sValue & """ FAILED") Return 0 EndIf EndIf EndIf Case $interaction = "get" Or $interaction = "read" $iEventerror = 0 $sTemp2 = $oElement.text If $iEventerror = 0 Then WriteToLog($sLogPath, "OK - """ & $interaction & """ interaction on element """ & $sTemp & ", Value: """ & StringStripWS($sTemp2, 3) & """") ; Test SetError(0, 0, 0) Return $sTemp2 Else WriteToLog($sLogPath, "ERROR - """ & $interaction & """ interaction on element """ & $sTemp & """ FAILED") SetError(-5, -5, -5) Return Null EndIf Case $interaction = "selectContextMenuItem" $iEventerror = 0 $oElement.selectContextMenuItem($sValue) If $iEventerror = 0 Then WriteToLog($sLogPath, "OK - """ & $interaction & """ interaction on element """ & $sTemp & " = " & $sValue) SetError(0, 0, 0) Return 1 Else SetError(-6, -6, -6) WriteToLog($sLogPath, "ERROR - """ & $interaction & """ interaction on element """ & $sTemp & """ FAILED") Return 0 EndIf Case $interaction = "pressToolbarContextButton" $iEventerror = 0 $oElement.pressToolbarContextButton($sValue) If $iEventerror = 0 Then WriteToLog($sLogPath, "OK - """ & $interaction & """ interaction on element """ & $sTemp & " = " & $sValue) SetError(0, 0, 0) Return 1 Else SetError(-7, -7, -7) WriteToLog($sLogPath, "ERROR - """ & $interaction & """ interaction on element """ & $sTemp & """ FAILED") Return 0 EndIf Case $interaction = "pressToolbarButton" $iEventerror = 0 $oElement.pressToolbarButton($sValue) If $iEventerror = 0 Then WriteToLog($sLogPath, "OK - """ & $interaction & """ interaction on element """ & $sTemp & " = " & $sValue) SetError(0, 0, 0) Return 1 Else SetError(-8, -8, -8) WriteToLog($sLogPath, "ERROR - """ & $interaction & """ interaction on element """ & $sTemp & """ FAILED") Return 0 EndIf Case $interaction = "selectColumn" $iEventerror = 0 $oElement.selectColumn($sValue) If $iEventerror = 0 Then WriteToLog($sLogPath, "OK - """ & $interaction & """ interaction on element """ & $sTemp & " = " & $sValue) SetError(0, 0, 0) Return 1 Else SetError(-8, -8, -8) WriteToLog($sLogPath, "ERROR - """ & $interaction & """ interaction on element """ & $sTemp & """ FAILED") Return 0 EndIf Case $interaction = "getAbsoluteRow" $tempArray = StringSplit($sValue, "|", 2) If UBound($tempArray) > 1 Then If StringUpper($tempArray[1]) = "TRUE" Or String($tempArray[1]) = "1" Then $oElement.getAbsoluteRow($tempArray[0]).selected = True If $oElement.getAbsoluteRow($tempArray[0]).selected <> True Then Return 0 WriteToLog($sLogPath, "ERROR - """ & $interaction & """ SET interaction on element """ & $sTemp & ", Row: " & $tempArray[0] & ", Value: " & StringUpper($tempArray[1])) Else WriteToLog($sLogPath, "OK - """ & $interaction & """ SET interaction on element """ & $sTemp & ", Row: " & $tempArray[0] & ", Value: " & StringUpper($tempArray[1])) Return 1 EndIf EndIf If StringUpper($tempArray[1]) = "FALSE" Or String($tempArray[1]) = "0" Then $oElement.getAbsoluteRow($tempArray[0]).selected = False If $oElement.getAbsoluteRow($tempArray[0]).selected <> False Then WriteToLog($sLogPath, "ERROR - """ & $interaction & """ SET interaction on element """ & $sTemp & ", Row: " & $tempArray[0] & ", Value: " & StringUpper($tempArray[1])) Return 0 Else WriteToLog($sLogPath, "OK - """ & $interaction & """ SET interaction on element """ & $sTemp & ", Row: " & $tempArray[0] & ", Value: " & StringUpper($tempArray[1])) Return 1 EndIf EndIf Else WriteToLog($sLogPath, "OK - """ & $interaction & """ READ interaction on element """ & $sTemp & ", Row: " & StringUpper($tempArray[0]) & ", Value: " & $oElement.getAbsoluteRow($tempArray[0]).selected) Return $oElement.getAbsoluteRow($sValue).selected EndIf Case Else $oElement.Visualize(True) If $bWrite2ConsoleL Then ConsoleWrite("ERROR - """ & $interaction & """ interaction on element """ & $elementText & """ (" & $uiElement & ") FAILED" & @CRLF) WriteToLog($sLogPath, "ERROR!!! - """ & $interaction & """ interaction DOES NOT EXIST!!!") SetError(-1, -1, -1) Return 0 EndSelect $oElement.Visualize(True) SetError(-1, -1, -1) Return 0 EndFunc ;==>SAP_ElementInteraction Func SAP_GetServerList($sType = "XML") ;-======================================================= ;-# Usage: SAP_GetServerList() # ;-# # ;-# This function will retreive the list of available # ;-# servers from SAP Logon configuration INI or XML # ;~# files. Location: '%appdata%\SAP\Common\' # ;-# # ;-#=====================================================# ;-# # ;-# $sType...- Get list from 'INI' or 'XML' file # ;-# Value can be 'INI' or 'XML', othervise # ;-# the -2 will be returned and will set # ;-# @error = -2 # ;-# # ;-# ! If no parameter is provided the function # ;-# it will try to locate XML file 1st and if # ;-# it does not exist it will check for INI # ;-# file automatically. # ;-# # ;-# ! If neither file is found -2 will be # ;-# returned and will be set to @error = -2. # ;-# # ;-# ! If any of XML or INI file is found, # ;-# alist, in form of the array, will be # ;-# returned. # ;-# # ;-#=====================================================# ;-# RETURN CODES # ;-# array - List or servers, sorted # ;-# -1 - Wrong $sType, only 'INI' or 'XML' values # ;-# are allowed! # ;-# -2 - File not found # ;-# -3 - Failed to return Array, check the file, # ;-# file coud be empty, function will report # ;-# the line number count from file in log. # ;-# # ;~# @extended values if Reurn code is -1 or -2 # ;-# -1 - Wrong $sType # ;-# -2 - File not found # ;-# -3 - Failed to return an array (list) # ;-# # ;-# ! If function fail to return the array # ;-# it will log the number of lines from # ;-# file, it could be empty! # ;-# # ;-#=====================================================# Local $aListResult, $aServices $path = @AppDataDir & "\SAP\Common\" If $sType = Default or $sType = "" Then $fileName = "SAPUILandscape.xml" If Not FileExists($path & $fileName) Then WriteToLog($sLogPath, "WARNING! - XML file: " & $fileName & " is MISSING, switching to INI type.") $fileName = "saplogon.ini" If Not FileExists($path & $fileName) Then WriteToLog($sLogPath, "ERROR!!! - File " & $fileName & " is MISSING") SetError(-2, -2, -2) Return -2 Else $sType = "INI" EndIf Else $sType = "XML" EndIf EndIf Select Case StringUpper($sType) = "INI" $fileName = "saplogon.ini" If FileExists($path & $fileName) Then WriteToLog($sLogPath, "OK - File " & $fileName & " LOCATED") $aListResult = IniReadSection($path & $fileName, "Description") _ArrayColDelete($aListResult, 0) _ArrayDelete($aListResult, 0) ;~ _ArrayDisplay($aListResult, "INI File") If IsArray($aListResult) Then WriteToLog($sLogPath, "OK - Returning list of servers, count: " & UBound($aListResult)) Return $aListResult Else WriteToLog($sLogPath, "ERROR - Unable to get list from the " & $fileName & _ " file, please check the file, numbers of lines in file: " & _ _FileCountLines($path & $fileName)) SetError(-3, -3, -3) Return -3 EndIf Else WriteToLog($sLogPath, "ERROR!!! - File " & $fileName & " is MISSING") SetError(-2, -2, -2) Return -2 EndIf Case StringUpper($sType) = "XML" $fileName = "SAPUILandscape.xml" $path = @AppDataDir & "\SAP\Common\" If FileExists($path & $fileName) Then WriteToLog($sLogPath, "OK - File " & $fileName & " LOCATED") Local $oXML = ObjCreate("Microsoft.XMLDOM") If Not IsObj($oXML) Then MsgBox(0, "", "Unable to create COM session to XML.") Exit EndIf $oXML.load($path & "SAPUILandscape.xml") $oParent = $oXML.selectSingleNode("//Landscape/Services") $aListResult = Null For $oChild In $oParent.childNodes For $oAttribute In $oChild.attributes If $oAttribute.nodename = "name" Then ;~ NodeName = $oChild.nodename ;~ AttributeName = $oAttribute.nodename ;~ AttributeValue = $oAttribute.text If $aListResult = "" or $aListResult = Null Then $aListResult = $oAttribute.text Else $aListResult = $aListResult & @TAB & $oAttribute.text EndIf ;~ ConsoleWrite("- " & $oAttribute.text & @CRLF) EndIf Next Next $aServices = StringSplit(String($aListResult), @TAB, 2) _ArraySort($aServices) ;~ _ArrayDisplay($aServices, "XML File") If IsArray($aListResult) Then WriteToLog($sLogPath, "OK - Returning list of servers, count: " & UBound($aListResult)) Return $aListResult Else WriteToLog($sLogPath, "ERROR - Unable to get list from the " & $fileName & _ " file, please check the file, numbers of lines in file: " & _ _FileCountLines($path & $fileName)) SetError(-3, -3, -3) Return -3 EndIf Else WriteToLog($sLogPath, "ERROR!!! - File " & $fileName & " is MISSING") SetError(-2, -2, -2) Return -2 EndIf Case Else WriteToLog($sLogPath, "ERROR!!! - File type: " & $sType & ", file name: " & $fileName & " is NOT SUPPORTED") SetError(-1, -1, -1) Return -1 EndSelect EndFunc Func RecursiveCheck($startPoint, $searchPath = "") Local $iDebugLevel = 0, $iElementFound = 0 Local $sPrefix = StringLeft($startPoint.id, StringInStr($startPoint.id, "/", 0, 4)) If $startPoint.ContainerType Then For $oSubElement In $startPoint.Children() If $iElementFound = 1 Then Return $iElementFound If $iDebugLevel > 0 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $oSubElement.Id = ' & StringTrimLeft($oSubElement.Id, 19) & @CRLF) If $sPrefix & $searchPath = $oSubElement.Id Then ;StringTrimLeft($oSubElement.Id,19) Then If $iDebugLevel > 0 Then ConsoleWrite("=====================================" & @CRLF) If $iDebugLevel > 0 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : ELEMENT FOUND' & @CRLF) ;### Debug Console If $iDebugLevel > 0 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $searchPath = ' & $searchPath & @CRLF) ;### Debug Console If $iDebugLevel > 0 Then ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $oSubElement.Id = ' & StringTrimLeft($oSubElement.Id, 19) & @CRLF) ;### Debug Console If $iDebugLevel > 0 Then ConsoleWrite("=====================================" & @CRLF) $iElementFound = 1 Return $iElementFound EndIf If $oSubElement.ContainerType Then RecursiveCheck($oSubElement, $searchPath) ;, $tempConnection, $oTempSession) EndIf Next EndIf EndFunc ;==>RecursiveCheck ;~ Logging Functions Func WriteToLog($sFilePath, $sValue, $iMode = 1, $iBorders = 0, $sBorderStyle = "=") ;~ ############################################################################# ;~ # $sFilePath - Full Path to the log file. If path if empty or "" # ;~ # file will be created on Desktop (SAP_Extend.log). # ;~ # $sValue - String to be written in the log file # ;~ # $iMode - FileOpen $iMode parameter, use it to create, append or # ;~ # to create new file with or without folder structure # ; # Please check the help file for FileOpen # ;~ # $border - Choose if you want to have a lie at the to, bottom or both # ;~ # 1 - lite will be added before a log line (above $sValue) # ;~ # 2 - lite will be added after a log line (below $sValue) # ;~ # 3 - lite will be added before and after a log line (1+2) # ;~ # $sBorderStyle - choose a char which will represent a line # ;~ # examples: -, =, #, +, ... # ;~ ############################################################################# Local $vResult If $bWriteToLog = False Then Return ;~ Local $bWrite2Console = True If $sFilePath = "" Or $sFilePath = -1 Or $SAP_Write_Default_Log = True Then _ $sFilePath = @DesktopDir & "\" & @YEAR & @MON & @MDAY & "-" & @HOUR & @MIN & @SEC & "_SAP_Default.log" If $iMode = Default Then $iMode = 1 If $bWrite2Console = True Then ConsoleWrite("> " & $sValue & @CRLF) Local $hLogFile = FileOpen($sFilePath, $iMode) If $hLogFile = -1 Then $sFilePath = @DesktopDir & "\" & @YEAR & @MON & @MDAY & "-" & @HOUR & @MIN & @SEC & "_SAP_Default.log" $SAP_Write_Default_Log = True FileWriteLine($sFilePath, $sValue) If Not FileExists($sFilePath) Then MsgBox(16, "Log Error", "Log file could not be created or accessed!!!" & @CRLF & @CRLF & _ "Path: " & $sFilePath) SetError(-1, -1, -1) Return -1 EndIf EndIf If $iBorders = 1 Or $iBorders = 3 Then FileWriteLine($hLogFile, _StringRepeat($sBorderStyle, 50)) $vResult = FileWriteLine($hLogFile, _NowCalc() & " - " & $sValue) If $vResult <> 1 Then MsgBox(16, "Log ERROR", "Error writing the line in the log file!!!" & @CRLF & @CRLF & _ "Path: " & $sFilePath & @CRLF & _ "Line: " & $sValue) SetError(-2, -2, -2) Return -2 EndIf If $iBorders = 2 Or $iBorders = 3 Then $vResult = FileWriteLine($hLogFile, _StringRepeat($sBorderStyle, 50)) If $vResult > 1 Then If $vResult <> 1 Then MsgBox(16, "Log ERROR", "Error writing the border in the log file!!!" & @CRLF & @CRLF & _ "Path: " & $sFilePath & @CRLF & _ "Line: " & _StringRepeat($sBorderStyle, 50)) SetError(-3, -3, -3) Return -3 EndIf EndIf FileClose($hLogFile) Return 1 EndFunc ;==>WriteToLog ;~ Misc Functions Func _msToTime($milisec, $includeMs = 1) Local $msec, $sec, $min, $hour, $time ;~ _TicksToTime($milisec,$hour, $min, $sec) $hour = Floor($milisec / 360000) $min = Floor(($milisec - $hour * 360000) / 60000) $sec = Floor(($milisec - $hour * 360000 - $min * 60000) / 1000) $msec = Floor($milisec - $hour * 360000 - $min * 60000 - $sec * 1000) If $hour < 10 Then $hour = "0" & $hour If $min < 10 Then $min = "0" & $min If $sec < 10 Then $sec = "0" & $sec $msec = _StringRepeat("0", 3 - StringLen($msec)) & $msec If $includeMs = 1 Then $time = $hour & ":" & $min & ":" & $sec & "." & $msec Else $time = $hour & ":" & $min & ":" & $sec EndIf Return $time EndFunc ;==>_msToTime Func _MS_StringToMultiDimArray($string, $rowSplit, $columnSplit) Local $n = 0, $array1, $array2, $finalArray[0], $aRow, $aCol If $rowSplit = @CRLF Then $rowSplit = @CR $string = StringReplace($string, @CRLF, @CR) EndIf $array1 = StringSplit($string, $rowSplit, 2) $aRow = UBound($array1) For $e = 0 To $aRow - 1 $array2 = StringSplit($array1[$e], $columnSplit, 2) $aCol = UBound($array2) If UBound($finalArray, 2) < $aCol Then _ ReDim $finalArray[$aRow][$aCol] For $n = 0 To $aCol - 1 $finalArray[$e][$n] = $array2[$n] Next Next Return $finalArray EndFunc ;==>_MS_StringToMultiDimArray ; Custom COM error handler Func ErrorCatch() $HexNumber = Hex($oMyError.number, 8) ConsoleWrite("- Error catched, $HexNumber: " & $HexNumber & @CRLF & _ "- WinDescription is: " & $oMyError.windescription & @CRLF) ConsoleWrite(@CRLF & "! COM Error occured:" & @CRLF & _ "! Error number is: " & $HexNumber & @CRLF & _ "! Windescription is: " & $oMyError.windescription & @CRLF & @CRLF) $iEventerror = 1 ; something to check for when this function returns EndFunc ;==>ErrorCatch