cancel
Showing results for 
Search instead for 
Did you mean: 

SAPGUI Scripting - Connection in Delphi/Lazarus

Former Member
0 Kudos

Hi,

i tried to convert my autoitscript projects to Delphi/Lazarus because i need a few things autoit can´t offer. I have a few scripts written that interact with the SAP GUI that work quite well, e.g. Excel data import, specific label printing (p-touch) automated data change/correction etc.

I thought the simple connection to the SAP-GUI scripting wouldn´t be that difficult to migrate but in fact i´m not able to just do the simple task to connect to the running SAP session in Delphi/Lazarus.

Perhaps anyone has already done this and could share the code in Delphi/Lazarus that does the following that works perfectly well in Visual Studio VBA:

Dim sapgui, sapapp, sap_connection, sap_session As Object

        sapgui = GetObject("SAPGUI")
        sapapp = sapgui.GetScriptingEngine
        sap_connection = sapapp.Children(0)
        sap_session = sap_connection.Children(0)

        MsgBox(sap_session.findById("wnd[0]").Text)

I tried a few things in Lazarus but it always sticks at the "Children(0)" connection with a ole type error

Thanks

Thomas

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hello Thomas,

look at the following piece of Lazarus/FPC code to control an active session:

uses

  ..., saprotwr_1_0_tlb, sapfewselib_1_0_tlb;

var

  MySapROTWrapper: ISapROTWrapper;

  MySapROTEntry: _Dsapfewse;

  MyGuiApplication: GuiApplication;

  MyGuiConnection: GuiConnection;

  MyGuiSession: GuiSession;

  MyGuiFrameWindow: GuiFrameWindow;

begin

  MySapROTWrapper := CoCSapROTWrapper.Create;

  MySapROTEntry := _Dsapfewse(MySapROTWrapper.GetROTEntry('SAPGUI'));

  MyGuiApplication := GuiApplication(MySapROTEntry.GetScriptingEngine);

  MyGuiConnection := GuiConnection(MyGuiApplication.Children.Item(0));

  MyGuiSession := GuiSession(MyGuiConnection.Children.Item(0));

  MyGuiFrameWindow := GuiFrameWindow(MyGuiSession.Children.Item(0));

  GuiTextField(MyGuiFrameWindow.findById('wnd[0]/tbar[0]/okcd', GuiTextField_)).Text_ := 'your transaction here';

  MyGuiFrameWindow.SendVKey(0);

  GuiTextField(MyGuiFrameWindow.findById('wnd[0]/usr/text field id here', GuiTextField_)).Text_ := 'text data';

  MyGuiFrameWindow.SendVKey(0);

...

and so on.

Best regards,

Sandro Cumerlato