How To…Separate File for Each Group

Question

“How do I export a separate file for each group in my report automatically?”

Solution

It is possible to automatically export each group inside a report as an individual file using the Group.NewFile property. See the steps below for more detailed instructions.

Starting with RB 11, the Group.NewFile property was added giving the user the ability to automatically export each group in a report to a separate file on disk. By default each group is given the original file name defined with the report followed by an underscore (_) and the break value of the group.

It is possible to manually assign each file name using the TppGroup.OnGetFileSuffix event.

Steps for exporting each group as a separate file
————————————————————-

1. Create a group using the groups dialog (Report | Groups… main menu option).

2. Check the “Create New File” option in the dialog before closing.

3. Set up the File Device properties as you would if you were exporting to a single file…

-AllowPrintToFile := True;
-DeviceType := ‘PDF’;
-ShowPrintDialog := False;
-TextFileName := ‘MyFile.pdf’;
4. If you would like to customize the name of each file created, implement the TppGroup.OnGetFileSuffix event. Assigning the aText parameter will alter the file suffix for each file.

-If you would like to take complete control over the entire file name for each exported file, set the TextFileName of the report equal to a single underscore “_”. This will then use the aText parameter as the entire file name.

5. Print the report.

Download: FileForEachGroup.zip

Sample Delphi code:

procedure TForm1.ppGroup1GetFileSuffix(Sender: TObject; var Text: string); 
begin 
  //This will change the file suffix for each group exported. 
  Text := ppReport1.DataPipeline['CustomerName']; 

end;