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