Error handling SAP script

Cano, Julius (uic86336) 21 Reputation points
2020-10-02T21:14:49.77+00:00

In the Display PO transaction in SAP the account assignment tab changes format. The first section of code captures the data in one format, the second will capture it in the alternate format. As the program is running when the frist section thros an error I need the program to disregard the error and pick up with the second section of code. If there is no error with the first section of code I need the program to disregard the second section. I have tried On Error Goto line1 (and inserted line1: below the first section but the second time through the loop it throws an error.

Cells(i, 21).Value = session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0019/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT12/ssubTABSTRIPCONTROL1SUB:SAPLMEVIEWS:1101/subSUB2:SAPLMEACCTVI:0100/subSUB1:SAPLMEACCTVI:1100/txtMEACCT1100-ABLAD").Text

 Cells(i, 21).Value = session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0019/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT12/ssubTABSTRIPCONTROL1SUB:SAPLMEVIEWS:1101/subSUB2:SAPLMEACCTVI:0100/subSUB1:SAPLMEACCTVI:1000/tblSAPLMEACCTVIDYN_1000TC/txtMEACCT1000-ABLAD[11,0]").Text

full code below ***********************************************************************************************


update Macro

'
'Catch and redirect errors in case SAP GUI is not
'open or not accessible
On Error GoTo NotConnected

'Get the SAP GUI Scripting object
Set SapGuiAuto = GetObject("SAPGUI")

'Get the currently running SAP GUI
Set SAPApp = SapGuiAuto.GetScriptingEngine

'Get the first system that is currently connected
Set SAPCon = SAPApp.Children(0)

'Get the first session (window) on that connection
Set session = SAPCon.Children(0)

'Return to regular error handling
On Error GoTo 0

'Your code here
'...
session.findById("wnd[0]").maximize

session.findById("wnd[0]/tbar[0]/okcd").Text = "ME23N"
session.findById("wnd[0]").sendVKey 0
Dim i As Integer

i = 2
Dim Data As Object

Dim x As Integer

  Application.ScreenUpdating = False
  ' Set numrows = number of rows of data.
  NumRows = Range("S2", Range("S2").End(xlDown)).Rows.Count
  ' Select cell a1.
  Range("S2").Select
  ' Establish "For" loop to loop "numrows" number of times.
  For x = 1 To NumRows

session.findById("wnd[0]/mbar/menu[0]/menu[0]").Select
session.findById("wnd[1]/usr/subSUB0:SAPLMEGUI:0003/ctxtMEPO_SELECT-EBELN").Text = Application.ActiveCell
session.findById("wnd[1]/tbar[0]/btn[0]").press

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

Cells(i, 20).Value = session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0019/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-EEIND[9,0]").Text

 Cells(i, 21).Value = session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0019/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT12/ssubTABSTRIPCONTROL1SUB:SAPLMEVIEWS:1101/subSUB2:SAPLMEACCTVI:0100/subSUB1:SAPLMEACCTVI:1100/txtMEACCT1100-ABLAD").Text


 Cells(i, 21).Value = session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0019/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT12/ssubTABSTRIPCONTROL1SUB:SAPLMEVIEWS:1101/subSUB2:SAPLMEACCTVI:0100/subSUB1:SAPLMEACCTVI:1000/tblSAPLMEACCTVIDYN_1000TC/txtMEACCT1000-ABLAD[11,0]").Text

i = i + 1

'
' Selects cell down 1 row from active cell.
ActiveCell.Offset(1, 0).Select

  Next
  Application.ScreenUpdating = True

Set session = Nothing

'Exception handling in case SAP GUI is not
'open or not accessible
Exit Sub

NotConnected:
MsgBox "Please log into SAP first.", vbCritical

End Sub

Community Center Not monitored
0 comments No comments
{count} votes

Accepted answer
  1. Script Man 81 Reputation points
    2020-10-07T06:02:31.253+00:00

    I would do the following:

    . . .
     on error resume next
     Cells(i, 21).Value = session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0019/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT12/ssubTABSTRIPCONTROL1SUB:SAPLMEVIEWS:1101/subSUB2:SAPLMEACCTVI:0100/subSUB1:SAPLMEACCTVI:1100/txtMEACCT1100-ABLAD").Text
    if err.number <> 0 then
    
          Cells(i, 21).Value = session.findById(&#34;wnd[0]/usr/subSUB0:SAPLMEGUI:0019/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT12/ssubTABSTRIPCONTROL1SUB:SAPLMEVIEWS:1101/subSUB2:SAPLMEACCTVI:0100/subSUB1:SAPLMEACCTVI:1000/tblSAPLMEACCTVIDYN_1000TC/txtMEACCT1000-ABLAD[11,0]&#34;).Text
    
    end if
    on error goto 0
    . . .
    

    Regards, ScriptMan

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Emi Zhang-MSFT 30,046 Reputation points Microsoft External Staff
    2020-10-05T01:58:17.103+00:00

    Hi @Cano, Julius (uic86336) ,
    Based on your description, this question is related to Excel develop, I suggest you post this issue to Excel for Developer forum:

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=exceldev

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.