SAP和Excel:在错误转到内?在错误转到内?

时间:2019-07-25 19:58:18

标签: excel vba

我正试图在我的SAP GUI和Excel电子表格之间来回切换。我有一个要在SAP中查看的表列表,要从SAP中提取数据,粘贴到Excel中,然后转到下一个表。如果该表在SAP中不存在,我希望它转到下一个表(该表当前可能不存在,但将来可能存在,并且我希望它是动态的,我不想对表进行硬编码名称)。

我已经有一个On Error GoTo工作序列,但是说我们要参考的下一张表也不存在;该错误将必须得到解决。

Sub SAPEverything()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

ans = MsgBox("Are you currently logged into SAP?", vbYesNoCancel)

If ans = vbNo Then
    MsgBox ("Please log into SAP, then come back to this macro.")
    Exit Sub
ElseIf ans = vbCancel Then
    Exit Sub
ElseIf ans = vbYes Then
    frmSAP.Show
    frmSAP.Hide

    LastRow = Sheets("Sheet2").Cells(Rows.Count, 19).End(xlUp).Row
    CurrRow = 2

    For i = 2 To LastRow
        Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object
        Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
        Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
        Set session = SAPCon.Children(0) 'Get the first session (window) on that connection
        'Start the transaction to view a table
        session.StartTransaction "Transaction"

        session.findById("wnd[0]").maximize
        session.findById("wnd[0]/tbar[1]/btn[16]").press
        session.findById("wnd[0]/tbar[1]/btn[8]").press
        On Error GoTo HandlingIt
        session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectAll
        session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
        session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItemByText "Copy Text"

        Workbooks("WorkbookName").Activate
        Sheets("Sheet2").Select
        Cells(CurrRow, 2).Select
        ActiveSheet.Paste
        NewLastRow = Sheets("Sheet2").Cells(Rows.Count, 2).End(xlUp).Row
        For k = CurrRow To NewLastRow
            Sheets("Sheet2").Cells(k, 1).Value = Sheets("Sheet2").Cells(i, 19).Value
        Next k
        CurrRow = NewLastRow + 1

    Next i
HandlingIt:
    currErr = i
    For i = currErr + 1 To LastRow
        Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object
        Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
        Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
        Set session = SAPCon.Children(0) 'Get the first session (window) on that connection
        'Start the transaction to view a table
        session.StartTransaction "Transaction"

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

        session.findById("wnd[0]/tbar[1]/btn[8]").press
        On Error GoTo HandlingIt
        session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectAll
        session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
        session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItemByText "Copy Text"

        Workbooks("WorkbookName").Activate
        Sheets("Sheet2").Select
        Cells(CurrRow, 2).Select
        ActiveSheet.Paste
        NewLastRow = Sheets("Sheet2").Cells(Rows.Count, 2).End(xlUp).Row
        For k = CurrRow To NewLastRow
            Sheets("Sheet2").Cells(k, 1).Value = Sheets("Sheet2").Cells(i, 19).Value
        Next k
        CurrRow = NewLastRow + 1

    Next i
End If

一旦我已经进入On Error GoTo部分,是否有可能引用代码的另一个On Error GoTo部分?甚至回到当前的“出错时转到”部分的开头?

4 个答案:

答案 0 :(得分:1)

将错误处理代码与“快乐之路”完全分开。您希望错误处理子例程中的代码仅在发生运行时错误时才执行,最重要的是,您要处理该错误–使用ABC指令。

Resume [label]

如果发现自己需要的不仅仅是一个错误处理子例程,那么您的过程正在做太多事情。将其分解为较小的过程,它们执行更少的操作,因此失败的原因更少。

答案 1 :(得分:0)

没有理由重复代码。如果您只想在遇到错误时跳过循环的其余部分,则可以完全做到这一点。

另一方面,在完成错误处理后,应始终重置OnError行为(通过运行OnError GoTo 0)。您不希望代码中的错误进一步神秘地将您带回到循环中。调试是噩梦。

    For i = 2 To LastRow
        Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object
        Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
        Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
        Set session = SAPCon.Children(0) 'Get the first session (window) on that connection
        'Start the transaction to view a table
        session.StartTransaction "Transaction"

        session.findById("wnd[0]").maximize
        session.findById("wnd[0]/tbar[1]/btn[16]").press
        session.findById("wnd[0]/tbar[1]/btn[8]").press
        On Error GoTo NextLoopIteration
        session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectAll
        session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
        session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItemByText "Copy Text"

        Workbooks("WorkbookName").Activate
        Sheets("Sheet2").Select
        Cells(CurrRow, 2).Select
        ActiveSheet.Paste
        NewLastRow = Sheets("Sheet2").Cells(Rows.Count, 2).End(xlUp).Row
        For k = CurrRow To NewLastRow
            Sheets("Sheet2").Cells(k, 1).Value = Sheets("Sheet2").Cells(i, 19).Value
        Next k
        CurrRow = NewLastRow + 1

NextLoopIteration:
        On Error GoTo 0
    Next i

答案 2 :(得分:0)

未经测试,可以这样做

Function sapObjectExist (session as object) as boolean
   On error goto EH
   session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectAll
   exit function
EH:
   sapObjectExist  = false
End Function

进一步阅读

SAP Documentation

Stefan's Homepage: Scritping Tracker

答案 3 :(得分:0)

如果发生错误,您可以跳到下一个循环迭代,并在继续下一个迭代之前重置错误处理程序。查看代码中的注释。

Sub SAPEverything()

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    ans = MsgBox("Are you currently logged into SAP?", vbYesNoCancel)

    If ans = vbNo Then
        MsgBox ("Please log into SAP, then come back to this macro.")
        Exit Sub
    ElseIf ans = vbCancel Then
        Exit Sub
    ElseIf ans = vbYes Then
        frmSAP.Show
        frmSAP.Hide

        LastRow = Sheets("Sheet2").Cells(Rows.Count, 19).End(xlUp).Row
        CurrRow = 2

        For i = 2 To LastRow
            Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object
            Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
            Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
            Set session = SAPCon.Children(0) 'Get the first session (window) on that connection
            'Start the transaction to view a table
            session.StartTransaction "Transaction"

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

            ' Enable error handler, in case of any error, execution will go to SkipIt.
            On Error GoTo SkipIt

            session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectAll
            session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
            session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItemByText "Copy Text"

            Workbooks("WorkbookName").Activate
            Sheets("Sheet2").Select
            Cells(CurrRow, 2).Select
            ActiveSheet.Paste
            NewLastRow = Sheets("Sheet2").Cells(Rows.Count, 2).End(xlUp).Row
            For k = CurrRow To NewLastRow
                Sheets("Sheet2").Cells(k, 1).Value = Sheets("Sheet2").Cells(i, 19).Value
            Next k

            CurrRow = NewLastRow + 1

SkipIt:
            On Error GoTo 0 ' Reset error handler.

        Next i

    End If
End Sub