I used to use VBA to automate SAP, now I use C# with SAPFEWSELib to control SAP. I can control Buttons or TextFields but I am doing something wrong with Scrollbars and don't know what the problem is.
My goal is to scroll bar but not in Table or GridView, in such cases I can avoid scrolling but the rules are the same, of course. I try to use SAPFEWSELib GuiScrollbar class. I am able to use GuiButton, GuiStatusbar, GuiSession and many others. My application is working properly and users run it on their machines so guess I use SAPFEWSELib in a proper way but I am not advanced in SAP and coding so the mistake can be really basic.
The error I get:
System.InvalidCastException: 'Unable to cast COM object of type 'System.__ComObject' to interface type 'SAPFEWSELib.GuiScrollbar'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{99CD4189-6B59-4596-B1DF-0C1121BB5240}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'
I use SAP recorder and get ID from VBS, then use it in my script. For types of object in SAp I have created my own classes, an example for a button:
public class SAPElementButton
{
GuiSession _session;
public string _Id;
public SAPElementButton(GuiSession session, string Id)
{
_session = session;
_Id = Id;
}
public void pressbutton()
{
{
GuiButton _SapButton = (GuiButton)_session.FindById(_session.Id.ToString() + _Id);
_SapButton.SetFocus();
_SapButton.Press();
}
}
}
Thanks to that I can create an object of my class where I give a path to be used by FindByID method in the class method. In the example above, I give the session and button id as a parameter for the constructor. In a logic part where I use method "pressbutton" I can avoid long ID which would make my code unrideable. It maybe seems a little bit complex and waisting time but let me manage complex logic. The code above works fine, I have attached it for example.
The problem is when I try to control Scrollbar, I get error like above:
public class SAPElementVerticalScrollbar
{
GuiSession _session;
public string _Id;
public SAPElementVerticalScrollbar(GuiSession session, string Id)
{
_session = session;
_Id = Id;
}
public void setScrollbar(int possition)
{
{
GuiScrollbar sAPElementVerticalScroll = (GuiScrollbar)_session.FindById(_session.Id.ToString() + _Id);
sAPElementVerticalScroll.Position=possition;
}
}
}
If an error would be that such Id unavailable, it is clear but syntax seems to be fine and object in SAP available.
Maybe I use wrong class from SAPFEWSELib or maybe there is another nice .dll libray I don't know to use instead of that one? Maybe there is a way to click "arrows" in ScrollBar like buttons? I hope someone can help me. I get detail if needed.enter image description here
It may be the case that you simply received the wrong object for your lookup.
You can check what kind of object you received by doing this:
object comObject = _session.FindById(_session.Id.ToString() + _Id);
string comType = Microsoft.VisualBasic.Information.TypeName(comObject);
// comType will now contain something like "GuiButton" or "GuiScrollbar"
This code requires a reference to the Microsoft.VisualBasic assembly.
If you really receive a GuiScrollbar it should work, I've seen success cases implemented similarly.
If you get something unusable, try casting the object to SAPFEWSELib.GuiVComponent
, then use the properties Left
and Top
to get the position of the scrollbar. These should be screen-relative coordinates in pixels.
You could then, theoretically, simulate mouse clicks by other means, e.g. similar to this: How do you simulate Mouse Click in C#?
During the time working with GUI scripting I have not seen any other fancy library, so you are likely stuck with SAPFEWSELib. The way we have handled this at my work place is to have a wrapper library that irons out some of the kinks in the scripting API. (I cannot share that unfortunately, as it is not publicly available)
To control such scrollbar we need to use object ISapTextFieldTarget, not GuiScrollbar. In ISapTextFieldTarget we will find properties: HorizontalScrollbar.Position and VerticalScrollbar.Position.
User contributions licensed under CC BY-SA 3.0