0

I use SAPFEWSELib to automate SAP. I have a problem to press a button from a drop down menu "as I guess :) ".

This code is auto generated by a SAP GUI script record. I need to reproduce this in C#:

session.findById("wnd[0]/shellcont/shell/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Emad Ali
  • 447
  • 1
  • 6
  • 11

2 Answers2

1

Solved.

var ctrl = SapSession.ActiveWindow.FindById("wnd[0]/shellcont/shell/shellcont/shell", false);
var shellToolbarContextButton = ((GuiShell)ctrl);
var btnToolbarContextButton = shellToolbarContextButton as GuiGridView;
btnToolbarContextButton?.PressToolbarContextButton("&MB_EXPORT");
Emad Ali
  • 447
  • 1
  • 6
  • 11
0

Another method to achieve the same results:

First, look at the code in the first answer as a reference to the static class utilized for my response here:How do I automate SAP GUI with c#.

Second, try the following line of code:

//Select Layout
        GuiGridView guiGridView = (GuiGridView) SAPActive.SapSession.FindById("wnd[0]/usr/cntlCCONTAINER/shellcont/shell");
        guiGridView.PressToolbarButton("&MB_VARIANT");

To make life easier and promote more productive coding, I created some basic methods within the SAPActive class that take in a string UI path and cast it to the desired object. Here are some examples:

    /**
     * Takes in a string UI path Id and returns a GuiTextField linked to the
     * current session. Utilizes the SAPActive class.
     */
    public static GuiTextField TextFieldPath(string path)
    {
        GuiTextField rtnField = (GuiTextField)SAPActive.SapSession.FindById(path);
        return rtnField;
    }
    /**
     * Takes in a string UI path Id and returns a GuiMenu linked to the
     * current session. Utilizes the SAPActive class.
     */
    public static GuiMenu MenuPath(string path)
    {
        GuiMenu rtnField = (GuiMenu)SAPActive.SapSession.FindById(path);
        return rtnField;
    }
    /**
     * Takes in a string UI path Id and returns a GuiFrameWindow linked to the
     * current session. Utilizes the SAPActive class.
     */
    public static GuiFrameWindow FrameWindowPath(string path)
    {
        GuiFrameWindow rtnField = (GuiFrameWindow)SAPActive.SapSession.FindById(path);
        return rtnField;
    }
    /**
     * Takes in a string UI path Id and returns a GuiButton linked to the
     * current session. Utilizes the SAPActive class.
     */
    public static GuiButton ButtonPath(string path)
    {
        GuiButton rtnField = (GuiButton)SAPActive.SapSession.FindById(path);
        return rtnField;
    }
    /**
     * Takes in a string UI path Id and returns a GuiGridView linked to the
     * current session. Utilizes the SAPActive class.
     */
    public static GuiGridView GridViewPath(string path)
    {
        GuiGridView rtnField = (GuiGridView) SAPActive.SapSession.FindById(path);
        return rtnField;
    }

Now, the above code can be changed to the following:

GuiGridView guiGridView = SAPActive.GridViewPath("wnd[0]/usr/cntlCCONTAINER/shellcont/shell");
guiGridView.PressToolbarButton("&MB_VARIANT"); 

This is a great reference: https://help.sap.com/viewer/b47d018c3b9b45e897faf66a6c0885a8/760.00/en-US/4af24c3281fb4d6a809e53238562d3b2.html

Adam Ziegler
  • 95
  • 1
  • 5