How To…Modify RAP Programs in Code

Question

“How do I modify existing RAP code without using the code workspace (i.e. in Delphi code)?”

Solution

Use the raGetCodeModule() routine to gain access to the code module object then loop through each program and global program using the Programs and AllGlobalPrograms lists.

Access the actual RAP code using the TraProgram.Source property.

Download: RAPModifyPrograms.zip

Sample Delphi code:

uses
  raCodMod,
  raClass;

procedure TForm1.btnConvertClick(Sender: TObject);
var
  lCodeModule: TraCodeModule;
  lProgram: TraProgram;
  liIndex: Integer;
  lsSourceString: String;
begin

  {get the code for the report}
  lCodeModule := raGetCodeModule(ppReport1);

  {event-handlers}
  for liIndex := 0 to lCodeModule.ProgramCount-1 do
    begin
      lProgram := lCodeModule.Programs[liIndex];

      lsSourceString := lProgram.Source;

      lsSourceString := StringReplace(lsSourceString, 'Hello', 'GoodBye', []);

      {do something here to modify the source and then re-assign it to the
        program}
      lProgram.Source := lsSourceString;


   end;


  {global programs}
  for liIndex := 0 to lCodeModule.AllGlobalProgramCount-1 do
    begin
      lProgram := lCodeModule.AllGlobalPrograms[liIndex];

      if (lProgram <> nil) then
        begin
          lsSourceString := lProgram.Source;

          lsSourceString := StringReplace(lsSourceString, 'Hello', 'GoodBye', []);

          {do something here to modify the source and then re-assign it to the program}
          lProgram.Source := lsSourceString;
          
        end;


   end;


end;