SAP: Unable to cast COM object of type 'System.__ComObject' to interface type 'sapfewse.GuiTextField'

-3

I am trying to interface with a SAP gui window using c#. Using this solution here: solution, I have successfully connected to my open GUI window. I can send the command

frame.Maximize();

which will maximize the window, as expected.

Trouble comes when I attempt to put some text into a text box.

((GuiTextField)session.FindById("wnd[0]/tbar[0]/okcd")).Text = "InputText";
    //or
    GuiTextField targetField =
    (GuiTextField)session.FindById("wnd[0]/tbar[0]/okcd");
    //or
    GuiTextField targetField =
    (GuiTextField)session.FindById("wnd[0]/tbar[0]/okcd", "GuiTextField");
    targetField.Text = "InputText";

The error I get is:

System.InvalidCastException
  HResult=0x80004002
  Message=Unable to cast COM object of type 'System.__ComObject' to interface type 'sapfewse.GuiTextField'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{B4D89EE3-6EFD-4F4C-9F42-AD42B71C8EB7}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

If I instead try this:

var x = session.FindById("wnd[0]/tbar[0]/okcd");

then my watch window tells me that x is of type GuiComponent, not GuiTextField as I hoped.

This will run:

GuiComponent targetField = (GuiComponent)session.FindById("wnd[0]/tbar[0]/okcd");

but then targetField doesn't have a text property.

What do i need to do to fix this? Thanks!

c#
asked on Stack Overflow Nov 29, 2018 by Phil_BHN • edited Feb 13, 2021 by Boghyon Hoffmann

1 Answer

0

Found the answer, leaving it here in case it can help someone else.

Looking at

GuiComponent targetField = (GuiComponent)session.FindById("wnd[0]/tbar[0]/okcd");

I found that targetField has a Type property:

var tmp = targetField.Type;

which turns out to be GuiOkCodeField.

So the required code is:

((GuiOkCodeField)session.FindById("wnd[0]/tbar[0]/okcd")).Text= "InputText";

tldr; use ((GuiOkCodeField)session.FindById("wnd[0]/tbar[0]/okcd")).Text= "InputText";

answered on Stack Overflow Dec 3, 2018 by Phil_BHN

User contributions licensed under CC BY-SA 3.0