【问题标题】:VBA to circumvent SAP disabled/non-clickable buttonsVBA 绕过 SAP 禁用/不可点击的按钮
【发布时间】: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

【问题讨论】:

    标签: vba excel sap


    【解决方案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
    

    【讨论】:

    • 非常感谢 Storax,您能告诉我如何设置“呼叫”或“转到”按钮按下功能吗? VBA 目前正在给我错误。我已经将该函数设置为一个单独的实体,但使用Call Buttonpressed(session) 不起作用。
    • 正如我在回答中建议的那样。在 else 之前删除所有内容并将其替换为 If Not ButtonPressed(session) 那么你会得到什么错误?也许我的英语不够好,但是“我已将功能设置为单独的实体”是什么意思,尤其是“作为单独的实体”。我从未听说过与 VBA 和函数相关的实体。
    • 嗨,Storax。我已将完整代码添加到原始消息中。不幸的是,我似乎无法真正将函数调用到代码中。我所说的“独立实体”的意思是它被描述为一个可以被调用的函数(所以不是专门放在这个子中)。
    • 请使用 Option Explicit 并声明你的变量。
    • 嗨,Storax,你能帮我解决这个问题吗?我应该如何声明我的变量,这将有助于连接到“buttonpressed”函数吗?很抱歉一直打扰您。
    猜你喜欢
    • 2020-08-17
    • 2019-09-28
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 2015-01-29
    相关资源
    最近更新 更多