SAP:无法将类型为“ System .__ ComObject”的COM对象转换为接口类型为“ sapfewse.GuiTextField”

时间:2018-11-29 12:35:16

标签: c# sap

我正在尝试使用c#与SAP gui窗口进行交互。 在solution处使用此解决方案,我已成功连接到打开的GUI窗口。 我可以发送命令

frame.Maximize();

将按预期最大化窗口。

当我尝试将一些文本放入文本框中时出现问题。

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

我得到的错误是:

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)).

如果我改为尝试此操作:

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

然后我的观察窗口告诉我x是GuiComponent类型,而不是我希望的GuiTextField类型。

这将运行:

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

但是targetField没有text属性。

该如何解决? 谢谢!

1 个答案:

答案 0 :(得分:0)

找到了答案,将其留在这里以防其他人得到帮助。

看着

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

我发现targetField具有Type属性:

var tmp = targetField.Type;

原来是GuiOkCodeField。

因此所需的代码是:

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

tldr;使用((GuiOkCodeField)session.FindById(“ wnd [0] / tbar [0] / okcd”))。Text =“ InputText”;

相关问题