cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

VBA - SAP Scripting ErrorHandling

Former Member
0 Likes
7,349

Hi everybody,


my question is regarding with error handling in VBA about SAP Scripting.
below you may see the my codes (which is not working properly)
I have tried many times to make it better but unfortunately I am failed

Shortly i would like to change the date of order in sap based on the excel new date and then copy previous date fronm SAP into the excel(existed delivery date)
May I ask your help to dig it better to know it ?

your help will be highly appreciated, thank you in advance.

Sub extractingPOOADataScriptd()

    Dim excelApp As Application
    Dim excelWb As ThisWorkbook
    Dim scriptWs As Worksheet
    Dim scriptData As Range
    Dim scriptRow As Range
    Dim session As Object
    Dim system As String
    Dim documentNumber As String
    Dim item As String
    Dim cell As Variant
    
    Set excelApp = Application
    Set excelWb = excelApp.ThisWorkbook
    Set scriptWs = excelWb.Sheets("Raw Data")
    
    Set scriptData = scriptWs.Range("A1").CurrentRegion
    Set scriptData = scriptData.Offset(1, 0).Resize(scriptData.Rows.Count - 1, scriptData.Columns.Count)
    
    excelApp.ScreenUpdating = False
    excelApp.ScreenUpdating = True


On Error Resume Next

    Set session = connectToOpenSAPSession
    session.findById("wnd[0]/tbar[0]/okcd").Text = "/nme23n"
    session.findById("wnd[0]").sendVKey 0

    For Each scriptRow In scriptData.Rows
        checking = scriptRow.Resize(1, 1).Offset(0, 7).Value
        documentNumber = scriptRow.Resize(1, 1).Value
        item = scriptRow.Resize(1, 1).Offset(0, 1).Value
        NewDate = scriptRow.Resize(1, 1).Offset(0, 2).Value


        
        If Right(item, 1) = "0" Then item = Left(item, Len(item) - 1)
        If Len(item) = 1 Then item = item
        If Len(item) = 2 Then item = item
        If Len(item) = 3 Then item = item


item = item - 1
If documentNumber <> "" Then


            session.findById("wnd[0]/tbar[1]/btn[17]").press
            session.findById("wnd[1]/usr/subSUB0:SAPLMEGUI:0003/ctxtMEPO_SELECT-EBELN").Text = documentNumber
            session.findById("wnd[1]/tbar[0]/btn[0]").press


        If IsObject(WScript) Then
        WScript.ConnectObject session, "on"
        WScript.ConnectObject Application, "on"
        End If
        session.findById("wnd[0]").maximize
        session.findById("wnd[0]/tbar[1]/btn[7]").press


scriptRow.Resize(1, 1).Offset(0, 6).Value = session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-EEIND[9," & item & "]").Text

If Err.Number <> 0 Then
scriptRow.Resize(1, 1).Offset(0, 6).Value = session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-EEIND[9," & item & "]").Text
scriptRow.Resize(1, 1).Offset(0, 7).Value = "Please check it to be ensure about changing."
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-EEIND[9," & item & "]").Text = NewDate
session.findById("wnd[0]/tbar[1]/btn[7]").press
Else
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-EEIND[9," & item & "]").Text = NewDate
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
scriptRow.Resize(1, 1).Offset(0, 7).Value = "date is updated"
session.findById("wnd[0]/tbar[1]/btn[7]").press
session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
End If


End If
Next scriptRow


    excelApp.ScreenUpdating = True
    excelApp.ScreenUpdating = True
End Sub

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Likes

Instead of

On error resume next

try maybe :

sub code()
On error goto error_hand:  ' to handle unexpected error
feedback ="unexpected error"
...
'code
if error here then
   feedack ="error here"
   Goto error_hand ' to handle expected error
end if
...
error_hand:
   msgbox feedback
   exit sub
done:
   msgbox "done"
   exit sub
end sub