Answered Crystal Report to RTF File

Hello,

I just migrate all my app that was using the crystal report OCX to a code that generate a pdf of the report:
Code:
/*------------------------------------------------------------------------------
  Purpose: Edition d'un rapport crystal au format PDF
  Parameters:  <none>
  Notes:       
------------------------------------------------------------------------------*/
DEFINE INPUT  PARAMETER rptNumenr AS CHARACTER   NO-UNDO.
DEFINE INPUT  PARAMETER rptChemin AS CHARACTER   NO-UNDO.
DEFINE INPUT  PARAMETER typChemin AS INTEGER     NO-UNDO.
DEFINE INPUT  PARAMETER pdfChemin AS CHARACTER   NO-UNDO.
DEFINE INPUT  PARAMETER visualisation AS LOGICAL     NO-UNDO.

{vignal/CRExportDestinationType.i}
{vignal/CRExportFormatType.i}

DEFINE VARIABLE crAppn      AS COM-HANDLE . /* Application Crystal Report */
DEFINE VARIABLE crReport    AS COM-HANDLE . /* Rapport Crystal */
DEFINE VARIABLE crExportOptions AS COM-HANDLE. /* Paramètre d'export */
DEFINE VARIABLE crSection   AS COM-HANDLE.
DEFINE VARIABLE crSubReport AS COM-HANDLE.
DEFINE VARIABLE crObject    AS COM-HANDLE.
DEFINE VARIABLE crParam     AS COM-HANDLE.
DEFINE VARIABLE crSubParam  AS COM-HANDLE.
DEFINE VARIABLE crParamFld  AS COM-HANDLE.

/* paramétre de connexion */
/*  base standard */
DEFINE VARIABLE cSrvBas AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cUseBas AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cPwdBas AS CHARACTER   NO-UNDO.
/*  base spé */
DEFINE VARIABLE cSrvSpe AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cUseSpe AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cPwdSpe AS CHARACTER   NO-UNDO.
DEFINE VARIABLE i AS INTEGER     NO-UNDO.
DEFINE VARIABLE j AS INTEGER     NO-UNDO.
DEFINE VARIABLE cmpt AS INTEGER     NO-UNDO.

/* récupération du chemin */
CASE typChemin :
    WHEN 1 THEN DO:
        FIND VCHEMIN WHERE VCHEMIN.checod = rptChemin NO-LOCK NO-ERROR.
        rptChemin = VCHEMIN.cheche .
    END.
    WHEN 3 THEN DO:
        FIND SYSPGM WHERE SYSPGM.pgmnom = rptChemin NO-LOCK NO-ERROR.
        rptChemin = SYSPGM.pgmchemin + SYSPGM.pgmnom.
    END.
END.


FIND FIRST SYSENV WHERE SYSENV.envcod = "CRYSTAL-REPORT":U NO-LOCK NO-ERROR. /* Paramètre de connection */
ASSIGN  /* Récupération des valeurs de la SYSENV */
    cSrvBas = ENTRY(1,SYSENV.envtxt,"|":U)
    cUseBas = ENTRY(2,SYSENV.envtxt,"|":U)
    cPwdBas = ENTRY(3,SYSENV.envtxt,"|":U).
IF NUM-ENTRIES(SYSENV.envtxt, "|":U) = 6 THEN ASSIGN /* Base spé */
    cSrvSpe = ENTRY(4,SYSENV.envtxt,"|":U)
    cUseSpe = ENTRY(5,SYSENV.envtxt,"|":U)
    cPwdSpe = ENTRY(6,SYSENV.envtxt,"|":U).

/* Création de l'application dans le com-handle */
CREATE "CrystalRunTime.Application" crAppn NO-ERROR.
/* Attribution du rapport à exploiter */
crReport = crAppn:OpenReport(rptChemin) NO-ERROR.

 /* Paramétrage de connexion de chaque table */
DO cmpt = 1 TO crReport:DATABASE:tables:COUNT:

    IF SUBSTRING(crReport:DATABASE:tables(cmpt):NAME, 1, 1) = "S":U THEN /* Si table lié à silv-exp */
        crReport:DATABASE:Tables(cmpt):ConnectionProperties("Password":U) = cPwdBas.
    ELSE ASSIGN
        crReport:DATABASE:Tables(cmpt):ConnectionProperties("Password":U) = cPwdSpe .
END.

crReport:DiscardSavedData.
crParam = crReport:ParameterFields.
DO cmpt = 1 TO NUM-ENTRIES(rptNumenr):
    crParamFld = crParam:ITEM(cmpt).
    NO-RETURN-VALUE crParamFld:AddCurrentValue(ENTRY(cmpt,rptNumenr)) .
END.
/*crReport:ParameterFields:GetItemByName("iddoc":U):AddCurrentValue(ENTRY(1,rptNumenr)) . /* On donne le numéro de doc a récupérer */*/

IF NUM-ENTRIES(rptNumenr) > 1 THEN /* Si présence de paramètre supplémentaire, on les ajoutes */
    DO cmpt = 3 TO NUM-ENTRIES(rptNumenr) :
       crReport:ParameterFields:GetItemByName("param":U + STRING(cmpt - 2)):AddCurrentValue(ENTRY(cmpt,rptNumenr)) .
    END.

DO i = 1 TO crReport:Sections:COUNT :
    crSection = crReport:Sections:ITEM(i) .
    DO j = 1 TO crSection:reportObjects:COUNT :
        crObject = crSection:reportObjects:ITEM(j).
        IF crObject:kind = 5 THEN DO:
            crSubReport = crObject:OpenSubreport .
            DO cmpt = 1 TO crSubReport:DATABASE:tables:COUNT:
                IF SUBSTRING(crSubReport:DATABASE:tables(cmpt):NAME, 1, 1) = "S":U THEN /* Si table lié à silv-exp */
                    crSubReport:DATABASE:Tables(cmpt):ConnectionProperties("Password":U) = cPwdBas.
                ELSE ASSIGN
                    crSubReport:DATABASE:Tables(cmpt):ConnectionProperties("Password":U) = cPwdSpe.
            END.
            crSubReport:DiscardSavedData.
            crSubParam = crSubReport:ParameterFields.
            DO cmpt = 1 TO NUM-ENTRIES(rptNumenr):
                crParamFld = crSubParam:ITEM(cmpt).
                NO-RETURN-VALUE crParamFld:AddCurrentValue(ENTRY(cmpt,rptNumenr)) .
            END.
            /*crSubReport:ParameterFields:GetItemByName("ss_iddoc":U):AddCurrentValue(ENTRY(1,rptNumenr)) . /* On donne le numéro de doc a récupérer */*/
        END.
        
    END.
END.

                                      
ASSIGN
    crExportOptions = crReport:ExportOptions
    crExportOptions:DiskFileName = pdfChemin
    crExportOptions:DestinationType = {&crEDTDiskFile}
    crExportOptions:FormatType = {&crEFTPortableDocFormat}
    crExportOptions:PDFExportAllPages = TRUE .
                                      
crReport:DisplayprogressDialog = FALSE.

crReport:EXPORT(FALSE) .
                                      

RELEASE OBJECT crExportOptions.
RELEASE OBJECT crReport.
RELEASE OBJECT crAppn.

ASSIGN
   crExportOptions = ?
   crReport = ?
   crAppn = ? .

IF visualisation THEN
    RUN utils/wviewdoc.w(pdfChemin,'Aperçu PDF') .

END PROCEDURE.
One of our user is complaining on this update becaused with the OCX version he was able to saved the report as a Reached Text File extention (.rtf) , action he cannont do now with the new version.
Do you know if there is a way of doing it?

Best Regards,

BobyIsProgress
 

Cecil

19+ years progress programming and still learning.
Hello,

I just migrate all my app that was using the crystal report OCX to a code that generate a pdf of the report:
Code:
/*------------------------------------------------------------------------------
  Purpose: Edition d'un rapport crystal au format PDF
  Parameters:  <none>
  Notes:     
------------------------------------------------------------------------------*/
DEFINE INPUT  PARAMETER rptNumenr AS CHARACTER   NO-UNDO.
DEFINE INPUT  PARAMETER rptChemin AS CHARACTER   NO-UNDO.
DEFINE INPUT  PARAMETER typChemin AS INTEGER     NO-UNDO.
DEFINE INPUT  PARAMETER pdfChemin AS CHARACTER   NO-UNDO.
DEFINE INPUT  PARAMETER visualisation AS LOGICAL     NO-UNDO.

{vignal/CRExportDestinationType.i}
{vignal/CRExportFormatType.i}

DEFINE VARIABLE crAppn      AS COM-HANDLE . /* Application Crystal Report */
DEFINE VARIABLE crReport    AS COM-HANDLE . /* Rapport Crystal */
DEFINE VARIABLE crExportOptions AS COM-HANDLE. /* Paramètre d'export */
DEFINE VARIABLE crSection   AS COM-HANDLE.
DEFINE VARIABLE crSubReport AS COM-HANDLE.
DEFINE VARIABLE crObject    AS COM-HANDLE.
DEFINE VARIABLE crParam     AS COM-HANDLE.
DEFINE VARIABLE crSubParam  AS COM-HANDLE.
DEFINE VARIABLE crParamFld  AS COM-HANDLE.

/* paramétre de connexion */
/*  base standard */
DEFINE VARIABLE cSrvBas AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cUseBas AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cPwdBas AS CHARACTER   NO-UNDO.
/*  base spé */
DEFINE VARIABLE cSrvSpe AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cUseSpe AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cPwdSpe AS CHARACTER   NO-UNDO.
DEFINE VARIABLE i AS INTEGER     NO-UNDO.
DEFINE VARIABLE j AS INTEGER     NO-UNDO.
DEFINE VARIABLE cmpt AS INTEGER     NO-UNDO.

/* récupération du chemin */
CASE typChemin :
    WHEN 1 THEN DO:
        FIND VCHEMIN WHERE VCHEMIN.checod = rptChemin NO-LOCK NO-ERROR.
        rptChemin = VCHEMIN.cheche .
    END.
    WHEN 3 THEN DO:
        FIND SYSPGM WHERE SYSPGM.pgmnom = rptChemin NO-LOCK NO-ERROR.
        rptChemin = SYSPGM.pgmchemin + SYSPGM.pgmnom.
    END.
END.


FIND FIRST SYSENV WHERE SYSENV.envcod = "CRYSTAL-REPORT":U NO-LOCK NO-ERROR. /* Paramètre de connection */
ASSIGN  /* Récupération des valeurs de la SYSENV */
    cSrvBas = ENTRY(1,SYSENV.envtxt,"|":U)
    cUseBas = ENTRY(2,SYSENV.envtxt,"|":U)
    cPwdBas = ENTRY(3,SYSENV.envtxt,"|":U).
IF NUM-ENTRIES(SYSENV.envtxt, "|":U) = 6 THEN ASSIGN /* Base spé */
    cSrvSpe = ENTRY(4,SYSENV.envtxt,"|":U)
    cUseSpe = ENTRY(5,SYSENV.envtxt,"|":U)
    cPwdSpe = ENTRY(6,SYSENV.envtxt,"|":U).

/* Création de l'application dans le com-handle */
CREATE "CrystalRunTime.Application" crAppn NO-ERROR.
/* Attribution du rapport à exploiter */
crReport = crAppn:OpenReport(rptChemin) NO-ERROR.

 /* Paramétrage de connexion de chaque table */
DO cmpt = 1 TO crReport:DATABASE:tables:COUNT:

    IF SUBSTRING(crReport:DATABASE:tables(cmpt):NAME, 1, 1) = "S":U THEN /* Si table lié à silv-exp */
        crReport:DATABASE:Tables(cmpt):ConnectionProperties("Password":U) = cPwdBas.
    ELSE ASSIGN
        crReport:DATABASE:Tables(cmpt):ConnectionProperties("Password":U) = cPwdSpe .
END.

crReport:DiscardSavedData.
crParam = crReport:ParameterFields.
DO cmpt = 1 TO NUM-ENTRIES(rptNumenr):
    crParamFld = crParam:ITEM(cmpt).
    NO-RETURN-VALUE crParamFld:AddCurrentValue(ENTRY(cmpt,rptNumenr)) .
END.
/*crReport:ParameterFields:GetItemByName("iddoc":U):AddCurrentValue(ENTRY(1,rptNumenr)) . /* On donne le numéro de doc a récupérer */*/

IF NUM-ENTRIES(rptNumenr) > 1 THEN /* Si présence de paramètre supplémentaire, on les ajoutes */
    DO cmpt = 3 TO NUM-ENTRIES(rptNumenr) :
       crReport:ParameterFields:GetItemByName("param":U + STRING(cmpt - 2)):AddCurrentValue(ENTRY(cmpt,rptNumenr)) .
    END.

DO i = 1 TO crReport:Sections:COUNT :
    crSection = crReport:Sections:ITEM(i) .
    DO j = 1 TO crSection:reportObjects:COUNT :
        crObject = crSection:reportObjects:ITEM(j).
        IF crObject:kind = 5 THEN DO:
            crSubReport = crObject:OpenSubreport .
            DO cmpt = 1 TO crSubReport:DATABASE:tables:COUNT:
                IF SUBSTRING(crSubReport:DATABASE:tables(cmpt):NAME, 1, 1) = "S":U THEN /* Si table lié à silv-exp */
                    crSubReport:DATABASE:Tables(cmpt):ConnectionProperties("Password":U) = cPwdBas.
                ELSE ASSIGN
                    crSubReport:DATABASE:Tables(cmpt):ConnectionProperties("Password":U) = cPwdSpe.
            END.
            crSubReport:DiscardSavedData.
            crSubParam = crSubReport:ParameterFields.
            DO cmpt = 1 TO NUM-ENTRIES(rptNumenr):
                crParamFld = crSubParam:ITEM(cmpt).
                NO-RETURN-VALUE crParamFld:AddCurrentValue(ENTRY(cmpt,rptNumenr)) .
            END.
            /*crSubReport:ParameterFields:GetItemByName("ss_iddoc":U):AddCurrentValue(ENTRY(1,rptNumenr)) . /* On donne le numéro de doc a récupérer */*/
        END.
      
    END.
END.

                                    
ASSIGN
    crExportOptions = crReport:ExportOptions
    crExportOptions:DiskFileName = pdfChemin
    crExportOptions:DestinationType = {&crEDTDiskFile}
    crExportOptions:FormatType = {&crEFTPortableDocFormat}
    crExportOptions:PDFExportAllPages = TRUE .
                                    
crReport:DisplayprogressDialog = FALSE.

crReport:EXPORT(FALSE) .
                                    

RELEASE OBJECT crExportOptions.
RELEASE OBJECT crReport.
RELEASE OBJECT crAppn.

ASSIGN
   crExportOptions = ?
   crReport = ?
   crAppn = ? .

IF visualisation THEN
    RUN utils/wviewdoc.w(pdfChemin,'Aperçu PDF') .

END PROCEDURE.
One of our user is complaining on this update becaused with the OCX version he was able to saved the report as a Reached Text File extention (.rtf) , action he cannont do now with the new version.
Do you know if there is a way of doing it?

Best Regards,

BobyIsProgress

Last time I touched Crystal Reports it was owned by Seagate .

Just doing a quick google search I found this.

Code:
          ExportOptions exportOpts = new ExportOptions();
          PdfRtfWordFormatOptions pdfRtfWordOpts = ExportOptions.CreatePdfRtfWordFormatOptions();
          DiskFileDestinationOptions destinationOpts = ExportOptions.CreateDiskFileDestinationOptions();
     
          pdfRtfWordOpts.FirstPageNumber = firstPage;
          pdfRtfWordOpts.LastPageNumber = lastPage;
          pdfRtfWordOpts.UsePageRange = useRange;
          exportOpts.ExportFormatOptions = pdfRtfWordOpts;
          exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat;
     
          destinationOpts.DiskFileName = fileName;
          exportOpts.ExportDestinationOptions = destinationOpts;
          exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
     
          reportDocument.Export (exportOpts);
 
Thank you @Cecil .
I forgot to closed the subject. I already add the solution with the include file that I found a while ago to make PDF from crystal.
I will put them in attached file.

If someone needed it :)
 

Attachments

  • CRExportFormatType.i
    1.2 KB · Views: 6
  • CRExportDestinationType.i
    345 bytes · Views: 7
Top