【发布时间】:2023-03-28 06:26:02
【问题描述】:
目前我正在编写一个宏(基于 Excel VBA)来自动在 SAP 中运行事务。不幸的是,有时按钮是不可按下的(即空白,见截图):
但我不知道如何将其构建到宏中(如果按钮不可按下/可点击,则继续下一步)。 这是按钮可按下/可点击的屏幕截图:
我有
On Error Resume Next
session.findById("wnd[0]/usr/tabsHEADER_TABSTRIP1/tabpTABS_OV_FC/ssubG_HEADER_SUBSCREEN1:SAPMV56A:1028/btnSCD_DISPLAY_1").Changeable = True
If Err.Number = 0 Then
session.findById("wnd[0]/usr/tabsHEADER_TABSTRIP1/tabpTABS_OV_FC/ssubG_HEADER_SUBSCREEN1:SAPMV56A:1028/btnSCD_DISPLAY_1").press
Else
aSheet.Cells(i, 3) = "No shipment calculation button"
aSheet.Cells(i, 4) = "Error"
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nVT02n"
session.findById("wnd[0]").sendVKey 0
GoTo begin_Loop
If "x".changeble=true 似乎不起作用(因为如果按钮是可点击的,它仍然会出现错误),If x.setfocus=true 也不起作用...我该使用哪个代码需要解决这个问题吗?
完整代码
Sub RUN_TEXT_UPDATE2()
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
Set aSheet = ActiveSheet
On Error GoTo err_handling
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nVT02N"
session.findById("wnd[0]").sendVKey 0
begin_Loop:
For i = 2 To aSheet.UsedRange.Rows.Count
If aSheet.Cells(i, 4).Value = "" Then
col1 = Trim(CStr(aSheet.Cells(i, 1).Value)) 'Column1 Shipment Number
col2 = Trim(CStr(aSheet.Cells(i, 2).Value)) 'Column2 New SCAC
'First entry screen
session.findById("wnd[0]/usr/ctxtVTTK-TKNUM").Text = col1 '"300428742"
session.findById("wnd[0]").sendVKey 0
'If the Consider the subsequent documents pop-up exists, automatically click it away
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]").sendVKey 0
End If
'Go to the "shipment cost calculation" tab
session.findById("wnd[0]/usr/tabsHEADER_TABSTRIP1/tabpTABS_OV_FC").Select
'If information pop-up exists, automatically click it away
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]").sendVKey 0
End If
'press the shipment calculation button
If Not ButtonPressed(session) Then
aSheet.Cells(i, 3) = "No shipment calculation button"
aSheet.Cells(i, 4) = "Error"
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nVT02n"
session.findById("wnd[0]").sendVKey 0
GoTo begin_Loop
'go into the shipment calculation
session.findById("wnd[0]/mbar/menu[0]/menu[1]").Select
session.findById("wnd[0]").sendVKey 0
'delete shipment
session.findById("wnd[0]/tbar[1]/btn[14]").press
'agree to delete it in the pop-up
session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nVT02N"
session.findById("wnd[0]").sendVKey 0
Resume begin_Loop
End If
'go back to VT02n
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nvt02n"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
'Go to tab "stages"
session.findById("wnd[0]/usr/tabsHEADER_TABSTRIP2/tabpTABS_OV_LG").Select
'If information pop-up exists, automatically click it away
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]").sendVKey 0
End If
'Change the SCAC number
session.findById("wnd[0]/usr/tabsHEADER_TABSTRIP2/tabpTABS_OV_LG/ssubG_HEADER_SUBSCREEN2:SAPMV56A:1032/subLEG_OVER:SAPLSTAG:1300/tblSAPLSTAGVTTS_ALL_CTRL/ctxtVTTS_ALL-TDLNR[3,0]").Text = col2 '"T030626"
session.findById("wnd[0]").sendVKey 0
'If information pop-up exists, automatically click it away
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]").sendVKey 0
End If
'save the order
session.findById("wnd[0]/tbar[0]/btn[11]").press
'If information pop-up exists, automatically click it away
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]").sendVKey 0
End If
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]").sendVKey 0
End If
'report the orderstatus back to excel
m_status = session.findById("wnd[0]/sbar").Text
If m_status <> "" Then
aSheet.Cells(i, 3) = m_status
aSheet.Cells(i, 4) = "UPDATED"
End If
End If
Next
next_SAP_Line:
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nVT02n"
session.findById("wnd[0]").sendVKey 0
MsgBox "All SCAC updated/changed"
MsgBox "Forza Ducati!"
Exit Sub
'If an error occurs, show that in the sheet, macro now crashes when there is a second error, still needs fix
err_handling: 'report the orderstatus back to excel
m_status = session.findById("wnd[0]/sbar").Text
aSheet.Cells(i, 3) = m_status
aSheet.Cells(i, 4) = "Error"
If i = 0 Then Resume begin_Loop '<--| if error happend before entering loop, the resume at loop start
'Resume next_SAP_Line '<--| if error happened inside loop, then resume at loop next itaration
Resume begin_Loop
End Sub
函数(放置在同一模块中,但在“End Sub”之后
Function ButtonPressed(session As Object) As Boolean
On Error GoTo NotPressed
session.findById("wnd[0]/usr/tabsHEADER_TABSTRIP1/tabpTABS_OV_FC/ssubG_HEADER_SUBSCREEN1:SAPMV56A:1028/btnSCD_DISPLAY_1").press
ButtonPressed = True
Exit Function
NotPressed:
ButtonPressed = False
End Function
【问题讨论】: