Search code examples
excelvbashellsap-gui

VBA to check if SAP GUI button menu entry is enabled


I need Excel to tell me if the button below called "Attachment List" is available to click or not, but I couldn't get it. Below is the code I tried:

Sub teste()
    
        Set SapGuiAuto = GetObject("SAPGUI")
        Set SAPApplication = SapGuiAuto.GetScriptingEngine
        Set SAPConnection = SAPApplication.Children(0)
        Set session = SAPConnection.Children(0)
        
    
            session.findById("wnd[0]").maximize
            session.findById("wnd[0]/tbar[0]/okcd").Text = "/nme53n"
            session.findById("wnd[0]").sendVKey 0
    
            session.findById("wnd[0]").sendVKey 17
            session.findById("wnd[1]/usr/subSUB0:SAPLMEGUI:0003/ctxtMEPO_SELECT-BANFN").Text = "Purchase Requisition"
            session.findById("wnd[1]/usr/subSUB0:SAPLMEGUI:0003/ctxtMEPO_SELECT-BANFN").caretPosition = 8
            session.findById("wnd[1]").sendVKey 0
    
        session.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
        Set botao = session.findById("/app/con[0]/ses[0]/wnd[0]/titl/shellcont/shell/")

End Sub

Attachment List Button

I set "botao" to get all the 8 button data from the list on the image, but none of the properties helped me.

I need something like this:

attach = botao.CurrentContextMenu.Children.Item(2).isfocused

The code botao.CurrentContextMenu.Children.Item(2) leads me to the "Attachment List" button, but no property was valuable to help me.

I REALLY need help with this.


Solution

  • In my test comes with a non-existent attachment a message below.

    If that is not the case with you, you could apply the following workaround, for example:

    
        ' The area at left of title is called the GOS container (shellcont).
        ' It contains a GuiToolbarControl (shell), with one button named %GOS_TOOLBOX,
        ' which is of type "ButtonAndMenu" (button with a dropdown menu).
        ' Click the dropdown part of the button.
    
        session.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
    
        ' Press the menu item "Attachment list"
    
        session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_VIEW_ATTA"
    
        ' A popup (wnd[1]) should open if there are attachments, otherwise none opens.
    
        on error resume next 
        session.findById("wnd[1]").close
        if err.number <> 0 then 
           msgbox "There are no attachments."
        end if
        on error goto 0
        ...
    

    Regards, ScriptMan