admin管理员组

文章数量:1405859

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?

本文标签: SAP GUI Scripting in DelphiStack Overflow