目前我正在编写一个宏(基于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
答案 0 :(得分:1)
也许您可以使用试图按下按钮的功能
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
您的代码会改为......
If Not ButtonPressed(session) Then
aSheet.Cells(i, 3) = "No shipment calculation button"
aSheet.Cells(i, 4) = "Error"
....
编辑,2017/05/02 你对属性Changeable的第一个想法应该有效。我想你只需要调整代码
Sub TestIt()
Dim sapApplication As Object
Dim session As Object
Dim SapGuiAuto As Object
Dim Connection As Object
If sapApplication Is Nothing Then
Set SapGuiAuto = GetObject("SAPGUI")
Set sapApplication = SapGuiAuto.GetScriptingEngine
End If
If Connection Is Nothing Then
Set Connection = sapApplication.Children(0)
End If
If session Is Nothing Then
Set session = Connection.Children(0)
End If
Dim sBtn As Object 'GuiButton
Set sBtn = session.FindById("wnd[0]/tbar[0]/btn[3]") ' Back button in the Toolbar
'Set sBtn = session.FindById("wnd[0]/tbar[0]/btn[11]") ' Save Button in the Toolbar
If sBtn.Changeable Then
Debug.Print "Can press button"
Else
Debug.Print "Cannot press button"
End If
End Sub