【发布时间】:2019-07-25 19:58:18
【问题描述】:
我试图在我的 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 部分?或者甚至回到当前 On Error GoTo 部分的开头?
【问题讨论】:
-
我有点困惑;如果您在一行中遇到错误,您是否只是想跳过该行的其余代码?
-
@JoshEller 是的,这就是我想要做的。我想去新行(下一个表名)
-
您应该事先检查该表是否存在。顺便说一句,你没有提到事务我们在谈论什么样的表,一个“普通”的 gui 表控件还是一个 alv 网格?
-
@Storax 在代码中还是在运行宏之前?如果是前者,我该怎么做?我是连接 SAP 和 Excel 的新手。我相信这是一个 alv 网格。
-
写一个以sapsession为变量的函数