VBA规避SAP禁用/不可点击的button

目前我正在编写一个macros(基于Excel VBA)在SAP中自动运行事务。 不幸的是,有时候一个button是不可压缩的(例如,截屏,见截图): 按钮不可按

但我不知道如何构build到macros(如果button不可按/可点击,继续下一步)。 以下是button可按/可点击的截图: 按钮可按下

我有

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似乎不起作用(因为如果button是可点击的,它仍然会出错),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 

function(放在同一模块中,但在“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 

也许你可以用一个试图按下button的函数来工作

 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