Skip to Content

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

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";//orGuiTextField targetField =(GuiTextField)session.FindById("wnd[0]/tbar[0]/okcd");//orGuiTextField targetField =(GuiTextField)session.FindById("wnd[0]/tbar[0]/okcd","GuiTextField");
    targetField.Text="InputText";

The error I get is:

System.InvalidCastExceptionHResult=0x80004002Message=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 (Exceptionfrom 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!

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

1 Answer

  • Best Answer
    Dec 03, 2018 at 11:46 AM

    Found the answer, leaving it here in case it can help someone else. I won't be offended if a mode deletes the whole question.

    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";

    Add comment
    10|10000 characters needed characters exceeded