Skip to Content
author's profile photo Former Member
Former Member

Error handling in CS02

Hello.
I am a newbie in SAP script and I have the following problem.
With the transaction CS02 I create bills of materials.
The script works very well as long as the material entered has a valid material master.
Now my problem.
If a suitable material master is not available, I would like to create a position type "T".
How can I catch the error at the marked position?

It doesn't work with "if err.number <> 0"

Thanks for your help

And here is the code i am using

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

' Excelblatt öffnen Anfang

Dim ExcelApp, ExcelWB, ExcelWSA0C

set ExcelApp=createobject("Excel.Application")

ExcelApp.visible=true

set ExcelWB=ExcelApp.Workbooks.Open("V:\BAAIN\U14\Stücklisten\Daten\StüLi.xls")

set ExcelWSA0C=ExcelWB.Worksheets("StüLi")

'Schleife über Koepfe

zeile = 2 ' Überschrift überspringen

ExcelWSA0C.Activate

Do While true 'Schleife über gesamtes Arbeitsblatt

KopfMat=ExcelWSA0C.Cells(zeile,1).value

If KopfMat = "" then

Exit do

End if

'Transaktion aufrufen

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

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

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

'Kopfdaten eingeben

session.findById("wnd[0]/usr/ctxtRC29N-MATNR").text = KopfMat

session.findById("wnd[0]/usr/ctxtRC29N-WERKS").text = "5001"

session.findById("wnd[0]/usr/ctxtRC29N-STLAN").text = "4"

session.findById("wnd[0]/usr/ctxtRC29N-STLAN").setFocus

session.findById("wnd[0]/usr/ctxtRC29N-STLAN").caretPosition = 0

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

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

'Schleife bis neues KopfMat kommt

hvar = 0 'Zähler für Positionen

Do While true

If KopfMat<>ExcelWSA0C.Cells(zeile,1).value Then

Exit Do

End If

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-POSTP[1," & CStr(hvar) &"]").text = "I"

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2," & CStr(hvar) &"]").text = ExcelWSA0C.Cells(zeile,3).value

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-MENGE[5," & CStr(hvar) &"]").text = ExcelWSA0C.Cells(zeile,5).value

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-MEINS[6," & CStr(hvar) &"]").text = ExcelWSA0C.Cells(zeile,6).value

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-SORTF[14,"& CStr(hvar) &"]").text = ExcelWSA0C.Cells(zeile,7).value

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-SORTF[14,0]").setFocus

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-SORTF[14,0]").caretPosition = 6

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

-------> error handling here

If err.number <> 0 Then

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-POSTP[1,"& CStr(hvar) &"]").text = "T"

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2,"& CStr(hvar) &"]").text = ""

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-MENGE[5,"& CStr(hvar) &"]").text = ExcelWSA0C.Cells(zeile,5).value

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-MEINS[6,"& CStr(hvar) &"]").text = ExcelWSA0C.Cells(zeile,6).value

session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-SORTF[14,"& CStr(hvar) &"]").text = ExcelWSA0C.Cells(zeile,7).value

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

session.findById("wnd[0]/usr/subPOS_PDAT:SAPLCSDI:0840/txtRC29P-POTX1").text = ExcelWSA0C.Cells(zeile,3).value

session.findById("wnd[0]/usr/subPOS_PDAT:SAPLCSDI:0840/txtRC29P-POTX2").text = ExcelWSA0C.Cells(zeile,4).value

session.findById("wnd[0]/usr/subPOS_PDAT:SAPLCSDI:0840/txtRC29P-POTX2").setFocus

session.findById("wnd[0]/usr/subPOS_PDAT:SAPLCSDI:0840/txtRC29P-POTX2").caretPosition = 15

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

End if

If hvar = 25 Then 'Ende des Eingabebereichs erreicht, nächste Seite aufrufen

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

hvar = 0

End if

hvar = hvar + 1

Zeile = Zeile + 1

loop

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

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

'Eingabe abschließen

Set SAPsession = Nothing

Set SAPConnection = Nothing

Set SAPGuiAuto = Nothing

loop

'ExcelWB.Close (True)

MsgBox ("Script erfolgreich durchgeführt.")

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Posted on May 20, 2013 at 08:44 AM

    Hello Patric.

    shortly before your loop start and object 'session' is already set insert this row:

    session.testtoolmode = 1

    This will disable all systemmessages and popups which may interrupt scripting.

    errorhandling can be done this way:

    '--------------------------------Code start-------------------------------------------------------

    '-------> error handling here
    If session.FindById("wnd[0]/sbar").MessageType = "E" Then

    session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-POSTP[1,"& CStr(hvar) &"]").text = "T"
    session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2,"& CStr(hvar) &"]").text = ""
    session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-MENGE[5,"& CStr(hvar) &"]").text = ExcelWSA0C.Cells(zeile,5).value
    session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-MEINS[6,"& CStr(hvar) &"]").text = ExcelWSA0C.Cells(zeile,6).value
    session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-SORTF[14,"& CStr(hvar) &"]").text = ExcelWSA0C.Cells(zeile,7).value
    session.findById("wnd[0]").sendVKey 0
    session.findById("wnd[0]/usr/subPOS_PDAT:SAPLCSDI:0840/txtRC29P-POTX1").text = ExcelWSA0C.Cells(zeile,3).value
    session.findById("wnd[0]/usr/subPOS_PDAT:SAPLCSDI:0840/txtRC29P-POTX2").text = ExcelWSA0C.Cells(zeile,4).value
    session.findById("wnd[0]/usr/subPOS_PDAT:SAPLCSDI:0840/txtRC29P-POTX2").setFocus
    session.findById("wnd[0]/usr/subPOS_PDAT:SAPLCSDI:0840/txtRC29P-POTX2").caretPosition = 15
    session.findById("wnd[0]").sendVKey 0

    End if

    '--------------------------------Code end--------------------------------------------------------

    This should get the statusbarmessage when it´s an error and proceed with your errorhandlingcode.

    Br, Holger

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 21, 2013 at 08:03 AM

    You can add error handling setting the On Error goto label and after this put a resume next

    see:

    http://www.microsoft.com/en-us/download/details.aspx?id=2764

    and this piece of code:

    Sub whatever()

    On Error resume error_handling

    ' do whatever you need

    ' ........

    'and before exiting disable error handling (if needed)

    On Error GoTo 0

    Exit Sub

    error_hanling:

    Msgbox "Something is odd .... "

    End Sub

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on May 21, 2013 at 09:58 AM

    Hi Patric

    a simple

        On error Resume next             If err.number <> 0 Then            session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-POSTP[1,"& CStr(hvar) &"]").text = "T"            session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-IDNRK[2,"& CStr(hvar) &"]").text = ""            session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-MENGE[5,"& CStr(hvar) &"]").text = ExcelWSA0C.Cells(zeile,5).value            session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/ctxtRC29P-MEINS[6,"& CStr(hvar) &"]").text = ExcelWSA0C.Cells(zeile,6).value            session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-SORTF[14,"& CStr(hvar) &"]").text = ExcelWSA0C.Cells(zeile,7).value            session.findById("wnd[0]").sendVKey 0            session.findById("wnd[0]/usr/subPOS_PDAT:SAPLCSDI:0840/txtRC29P-POTX1").text = ExcelWSA0C.Cells(zeile,3).value            session.findById("wnd[0]/usr/subPOS_PDAT:SAPLCSDI:0840/txtRC29P-POTX2").text = ExcelWSA0C.Cells(zeile,4).value            session.findById("wnd[0]/usr/subPOS_PDAT:SAPLCSDI:0840/txtRC29P-POTX2").setFocus            session.findById("wnd[0]/usr/subPOS_PDAT:SAPLCSDI:0840/txtRC29P-POTX2").caretPosition = 15            session.findById("wnd[0]").sendVKey 0        End ifon error goto 0

    will do the trick ..

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.