Automate Exporting of Crystal Reports RPT to PDF

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Automate Exporting of Crystal Reports RPT to PDF

Post by Marcus Tettmar » Wed Nov 15, 2006 9:20 am

This version opens a report in Crystal and exports it to PDF:

Code: Select all

//Setup Input File:
Let>inReport=c:\reports\somereport.rpt

//Configure output file:
Let>outFile=c:\exports\somereport.pdf

//Open the report in Crystal
ExecuteFile>inReport
WaitWindowOpen>Crystal Reports - [*
WaitReady>0

//Wait while Status bar says "Formatting"
Label>IsReady
  GetControlText>Crystal Reports - [*,Afx:00400000:8:00010011:00000010:00000000,1,Status
  Pos>Formatting,status,1,p
  If>p>0,IsReady

//Now ready to export - Send ALT-f-e-e to initiate File/Export/Export
Wait>2
Press ALT
Send>fee
Release ALT
WaitWindowOpen>Export

//In my case PDF is first option, so no need to change the option, just press Enter
SetFocus>Export
Press Enter

//Wait for next "Export Options" window to appear and press enter again
WaitWindowOpen>Export Options
SetFocus>Export Options
Press Enter

//Wait for "Choose export file" dialog and provide the output file
WaitWindowOpen>Choose export file
SetFocus>Choose export file
Send>outFile
Press Enter

//Now wait for the "Exporting Records" progress window
WaitWindowOpen>Exporting Records

//And then wait for the "Exporting Records" progress window to disappear
WaitWindowClosed>Exporting Records

//All done - quit Crystal?
SetFocus>Crystal Reports - [*
Press ALT
Press F4
Release ALT
This next version has been modified to export a large number of reports. The main code has been moved to a subroutine and the import/export files are read in from a text file:

Code: Select all

// create a text file with the following format
// inputfile1.rpt;outputfile1.pdf
// inputfile2.rpt;outputfile2.pdf
// inputfile3.rpt;outputfile3.pdf
// etc;etc

//Set path of input file here:
Let>inputFile=d:\input.txt

//this part loops through the input file, extracts each input/output pair
//and calls the DoExport subroutine to process the export
Let>k=1
Label>ReadInputLoop
  ReadLn>inputFile,k,line
  If>line=##EOF##,finish
  Separate>line,;,parts
  Let>inReport=parts_1
  Let>outFile=parts_2
  GoSub>DoExport
  Let>k=k+1
Goto>ReadInputLoop
Label>finish

SRT>DoExport
  //Open the report in Crystal
  ExecuteFile>inReport
  WaitWindowOpen>Crystal Reports - [*
  WaitReady>0

  //Wait while Status bar says "Formatting"
  Label>IsReady
    GetControlText>Crystal Reports - [*,Afx:00400000:8:00010011:00000010:00000000,1,Status
    Pos>Formatting,status,1,p
    If>p>0,IsReady

  //Now ready to export - Send ALT-f-e-e to initiate File/Export/Export
  Wait>2
  Press ALT
  Send>fee
  Release ALT
  WaitWindowOpen>Export

  //In my case PDF is first option, so no need to change the option, just press Enter
  SetFocus>Export
  Press Enter

  //Wait for next "Export Options" window to appear and press enter again
  WaitWindowOpen>Export Options
  SetFocus>Export Options
  Press Enter

  //Wait for "Choose export file" dialog and provide the output file
  WaitWindowOpen>Choose export file
  SetFocus>Choose export file
  Send>outFile
  Press Enter

  //Now wait for the "Exporting Records" progress window
  WaitWindowOpen>Exporting Records

  //And then wait for the "Exporting Records" progress window to disappear
  WaitWindowClosed>Exporting Records

  //All done - quit Crystal?
  SetFocus>Crystal Reports - [*
  Press ALT
  Press F4
  Release ALT
End>DoExport

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts