3

I am developing an application in Delphi that needs to interact with SAP GUI via Scripting to automate some material control operations. However, I am facing difficulties in establishing the correct communication with SAP GUI Scripting and executing the desired commands.

Scenario:

  • I am using Delphi 12.2 as my programming language.
  • SAP GUI Scripting is enabled in the SAP GUI settings and on the server.
  • The objective is to connect to SAP GUI, navigate between screens, fill in fields automatically, process transactions, and extract reports.
  • I have tried using the SAP Scripting API according to documentation and examples found online, but I still encounter errors.

Issues faced:

  • Error when trying to access the session object: some attempts return errors like

    Object required

    or

    ActiveX component can't create object

  • Code works in VBA but not in Delphi: I tested some scripts in VBA (Excel) and in a VBS file, and they work fine. However, when porting to Delphi, I cannot reproduce the same behavior.

Example of code used:

uses
  ComObj, Variants;

procedure TForm1.Button1Click(Sender: TObject);
var
  SAPGuiAuto, Application, Connection, Session: OleVariant;
begin
  try
    SAPGuiAuto := GetActiveOleObject('SAPGUI.ScriptingCtrl.1');
    //SAPGuiAuto := CreateOleObject('SAPGUI.ScriptingCtrl.1');
    Application := SAPGuiAuto.GetScriptingEngine;
    Connection := Application.Children(0);
    Session := Connection.Children(0);
    
    // Test: Access a transaction
    Session.StartTransaction('MMBE');
  except
    on E: Exception do
      ShowMessage('Err: ' + E.Message);
  end;
end;

Questions:

  1. Am I referencing the objects correctly?
  2. Is there any Delphi-specific detail I should be aware of?
  3. Is there any additional configuration that needs to be done in SAP GUI or Delphi to allow this communication?
7
  • 2
    I am not sure enough to write an answer, but a few points to consider: (1) Your code looks good, but depending on what "Children" is - a function or array - you may need to use Chidren(0) or Children[0]. (2) You might need to call CoInitialize at the start of your application (or in each thread, if you use multiple threads) and CoUninitialize when the application closes down. If your program is a VCL application with just one thread, then this is done automatically for you by TApplication, but otherwise you need to do it manually yourself. (3) Nothing on the Delphi side that I can think of. Commented Mar 7 at 9:33
  • Thank you for your response! About Children(0) vs Children[0]: I will test both approaches to see if there is any difference in behavior. About CoInitialize and CoUninitialize: My application is VCL and runs on a single thread, so as I understand it, COM initialization should already be handled automatically by TApplication. Since the code works in VBA/VBS but not in Delphi, there might be some subtle difference in how COM objects are handled. Do you see any possibility that this could be related to how Delphi manages variants (OleVariant)? I appreciate any further insights you may have! Commented Mar 7 at 12:10
  • 2
    I actually think that Children[0] is the required syntax, because Children seems to be a GuiComponentCollection (I googled for SAPGUI.ScriptingCtrl.1). Probably you're aware, but I'll mention it anyway: as opposed to Delphi's normal strict type checking at compile time, type or syntax checking does not occur on OLE variants. The calls are attempted at run-time. So "anything" compiles and the error at run-time is not always clear. Commented Mar 7 at 12:43
  • 1
    I have not used SAP scripting, but Excel, and have solved the "reuse existing or create new?" issue like this: var XL: OleVariant; begin try XL := GetActiveOleObject('Excel.Application'); except on EOleSysError do {Probably Excel was not running} begin XL := CreateOleObject('Excel.Application'); XL.Visible := True; end; end; { ... } end; Commented Mar 7 at 12:45
  • Thank you again for your help. I tested both Children[0] and Children(0), but the runtime error persists. I also tried mimicking the Excel approach by attempting to get an active SAP GUI instance before creating one, yet the issue remains. It seems the problem lies deeper in how Delphi handles OLE automation with SAP GUI Scripting. I appreciate your help. Commented Mar 8 at 4:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.