cancel
Showing results for 
Search instead for 
Did you mean: 

"Unknown runtime error" in my script

neto
Explorer
0 Kudos

Hello, I am mechanical engineer, and I do not have extensive knowledge in programming. Searching about script, I found one and adapted to my need. Below de code.  

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

REM ADDED BY EXCEL *************************************

Dim objExcel

Dim objSheet, intRow, i

Set objExcel = GetObject(,"Excel.Application")

Set objSheet = objExcel.ActiveWorkbook.ActiveSheet

For i = 2 to objSheet.UsedRange.Rows.Count

COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1

COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2

COL3 = Trim(CStr(objSheet.Cells(i, 3).Value)) 'Column3

REM ADDED BY EXCEL *************************************

session.findById("wnd[0]/tbar[0]/okcd").text = "IE01"

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

session.findById("wnd[0]/usr/ctxtRM63E-EQUNR").text = COL1

session.findById("wnd[0]/usr/ctxtRM63E-REFEQ").text = "VEM037829101"

session.findById("wnd[0]/usr/ctxtRM63E-REFEQ").setFocus

session.findById("wnd[0]/usr/ctxtRM63E-REFEQ").caretPosition = 10

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

session.findById("wnd[1]").sendVKey 0

session.findById("wnd[0]/usr/subSUB_EQKO:SAPLITO0:0152/subSUB_0152B:SAPLITO0:1525/txtITOB-SHTXT").text = COL2

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\01/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102C:SAPLITO0:1022/txtITOB-HERST").text = ""

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\01/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102C:SAPLITO0:1022/txtITOB-HERST").setFocus

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\01/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102C:SAPLITO0:1022/txtITOB-HERST").caretPosition = 0

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02").select

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/ctxtITOB-ABCKZ").text = "B"

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/ctxtITOB-ABCKZ").setFocus

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/ctxtITOB-ABCKZ").caretPosition = 1

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03").select

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1052/ctxtITOB-ANLNR").text = ""

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1052/ctxtITOB-ANLNR").setFocus

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1052/ctxtITOB-ANLNR").caretPosition = 0

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\04").select

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\04/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1060/subSUB_1060A:SAPLITO0:1065/btnFCODE_CIPL").press

session.findById("wnd[1]/tbar[0]/btn[14]").press

session.findById("wnd[1]/usr/ctxtIEQINSTALL-HEQNR").text = COL3

session.findById("wnd[1]/usr/txtIEQINSTALL-HSTPS").text = "VE"

session.findById("wnd[1]/usr/txtIEQINSTALL-HSTPS").setFocus

session.findById("wnd[1]/usr/txtIEQINSTALL-HSTPS").caretPosition = 2

session.findById("wnd[1]/tbar[0]/btn[16]").press

session.findById("wnd[0]/tbar[0]/btn[11]").press

REM FINALIZATION CONTROL CHECK ************************

aux=col1 & " " & col2 & " " & col3  & " " & col4 & " " & col5  & " " & col6

CreateObject("WScript.Shell").run("cmd /c @echo %date% %time% " & aux & " >> C:\Users\482145\Desktop\Eficiencia energética\PlOrCreationLog.txt")

next

msgbox "Process Completed"

REM FINALIZATION CONTROL CHECK ************************

But is occurring the following error. Its like "Unknown runtime error". I have two SAP windows open.

Accepted Solutions (0)

Answers (2)

Answers (2)

neto
Explorer
0 Kudos

I copied the beginning of the code and pasted in my script. Now appears erro in line 21.

holger_khn
Contributor
0 Kudos

Hello.

I have modified your code a Little bit.

Normally I would suggest to implement code in Excel macro as you want to get data from Excel as well. But as you just have Basic programming skills we should mve on with vbs-file.

If you got an error again please post not only line number. As well Show us which line this is in your code. As well if that is in FOR-NEXT Loop it would be great if i=2 (Initial) or higher. Thanks.

'CODE start-------------------------------------------------------------------------------------------------------------------------------------

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

REM ADDED BY EXCEL *************************************

Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(,"Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet

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

For i = 2 to objSheet.UsedRange.Rows.Count

COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1
COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2
COL3 = Trim(CStr(objSheet.Cells(i, 3).Value)) 'Column3

REM ADDED BY EXCEL *************************************

session.findById("wnd[0]/tbar[0]/okcd").text = "/nIE01"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRM63E-EQUNR").text = COL1
session.findById("wnd[0]/usr/ctxtRM63E-REFEQ").text = "VEM037829101"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[0]/usr/subSUB_EQKO:SAPLITO0:0152/subSUB_0152B:SAPLITO0:1525/txtITOB-SHTXT").text = COL2
session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\01/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102C:SAPLITO0:1022/txtITOB-HERST").text = ""
session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02").select
session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/ctxtITOB-ABCKZ").text = "B"
session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03").select
session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1052/ctxtITOB-ANLNR").text = ""
session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\04").select
session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\04/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1060/subSUB_1060A:SAPLITO0:1065/btnFCODE_CIPL").press
session.findById("wnd[1]/tbar[0]/btn[14]").press
session.findById("wnd[1]/usr/ctxtIEQINSTALL-HEQNR").text = COL3
session.findById("wnd[1]/usr/txtIEQINSTALL-HSTPS").text = "VE"
session.findById("wnd[1]/tbar[0]/btn[16]").press
session.findById("wnd[0]/tbar[0]/btn[11]").press

REM FINALIZATION CONTROL CHECK ************************

aux=col1 & " " & col2 & " " & col3  & " " & col4 & " " & col5  & " " & col6
CreateObject("WScript.Shell").run("cmd /c @echo %date% %time% " & aux & " >> C:\Users\482145\Desktop\Eficiencia energética\PlOrCreationLog.txt")
Next

msgbox "Process Completed"

REM FINALIZATION CONTROL CHECK ************************

'CODE end---------------------------------------------------------------------------------------------------------------------------------------

neto
Explorer
0 Kudos

Now the error appear in line 19.

Set objSheet = objExcel.ActiveWorkbook.ActiveSheet

All code:

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

REM ADDED BY EXCEL *************************************

Dim objExcel

Dim objSheet, intRow, i

Set objExcel = GetObject(,"Excel.Application")

Set objSheet = objExcel.ActiveWorkbook.ActiveSheet

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

For i = 3 to objSheet.UsedRange.Rows.Count

COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1

COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2

COL3 = Trim(CStr(objSheet.Cells(i, 3).Value)) 'Column3

REM ADDED BY EXCEL *************************************

session.findById("wnd[0]/tbar[0]/okcd").text = "/nIE01"

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

session.findById("wnd[0]/usr/ctxtRM63E-EQUNR").text = COL1

session.findById("wnd[0]/usr/ctxtRM63E-REFEQ").text = "VEM037829101"

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

session.findById("wnd[1]").sendVKey 0

session.findById("wnd[0]/usr/subSUB_EQKO:SAPLITO0:0152/subSUB_0152B:SAPLITO0:1525/txtITOB-SHTXT").text = COL2

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\01/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102C:SAPLITO0:1022/txtITOB-HERST").text = ""

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02").select

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/ctxtITOB-ABCKZ").text = "B"

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03").select

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1052/ctxtITOB-ANLNR").text = ""

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\04").select

session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\04/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1060/subSUB_1060A:SAPLITO0:1065/btnFCODE_CIPL").press

session.findById("wnd[1]/tbar[0]/btn[14]").press

session.findById("wnd[1]/usr/ctxtIEQINSTALL-HEQNR").text = COL3

session.findById("wnd[1]/usr/txtIEQINSTALL-HSTPS").text = "VE"

session.findById("wnd[1]/tbar[0]/btn[16]").press

session.findById("wnd[0]/tbar[0]/btn[11]").press

REM FINALIZATION CONTROL CHECK ************************

aux=col1 & " " & col2 & " " & col3  & " " & col4 & " " & col5  & " " & col6

CreateObject("WScript.Shell").run("cmd /c @echo %date% %time% " & aux & " >> C:\Users\482145\Desktop\Eficiencia energética\PlOrCreationLog.txt")

Next

msgbox "Process Completed"

REM FINALIZATION CONTROL CHECK ************************

holger_khn
Contributor
0 Kudos

This is caused by missing link to active workbook.

So you Need to open workbook via vbs-script and set an object. Then you can handle this via script.

One of the reasons why I insert my code in Excel Macro when I want to use data from Excel as Input.

Dim objExcel,ObjWorkbook,objsheet

Set objExcel = CreateObject("Excel.Application")

Set objWorkbook = objExcel.Workbooks.Open("C:\yourpath\Folder\excelfile_name.xls")

set objsheet = objExcel.ActiveWorkbook.Worksheets(1)

msgbox objsheet.Cells(1,1).value

objExcel.ActiveWorkbook.Close

objExcel.Workbooks.Close

objExcel.Application.Quit

That is an example how to bind an worksheet from Excel workbook for usage in vbs-script

script_man
Active Contributor
0 Kudos

Hi Carlos,

Find out first where the fault is located. Start your VBScript from Windows Explorer by double-clicking with the mouse. Then comes a window where appears the error line. Then you can look further.

Regards,

ScriptMan

neto
Explorer
0 Kudos

Thank you for the help Script Man. The error is in the line 6, caract 4. But I don't no the meaning of this line, because I just copied and pasted it. ;/

holger_khn
Contributor
0 Kudos

Have you test to make an script recording (e.g. just open Transaction IE03 and open an existing Equipment).

Then go back to sap Initial Screen and call recorded script.

If that work compare first lines of recorded script with your faulty script. If you see any differences.

neto
Explorer
0 Kudos

Hello Holger Kohn, thank for your help. I tested now and it worked. Follow the code below.

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]/tbar[0]/okcd").text = "ie03"

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

session.findById("wnd[0]/usr/ctxtRM63E-EQUNR").text = "ebl037829101"

session.findById("wnd[0]/usr/ctxtRM63E-EQUNR").caretPosition = 12

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

session.findById("wnd[0]/tbar[0]/btn[3]").press

session.findById("wnd[0]/tbar[0]/btn[3]").press