How to open a Crystall Report XI with VBScript

Posted on 2007-08-08
Last Modified: 2013-11-05
We have an existing VBScript that use ocx to execute Crystal report 6 and export them to plain text format.  I did some research and found out that ocx is no longer used and it have been replaced with rdc.  Most of the sample code were related to VB.  Can anyone provide some sample VBScript  code on how to execute Crystal Report XI?
Question by:edmondwu
    LVL 42

    Accepted Solution


    Author Comment

    Thanks for your help frodoman!
    I finally able to export plain text crystal report 10 or XI with VBScript.
    Option Explicit

    WScript.Echo "START: " & Date & " " & Time
    WScript.Echo ""

    Dim crApp
    Dim crRpt

    Dim intI, dateDate, dateTime, strRunDate, strRunTime
    Dim objArgs, strArgs, strParseArray, strArg_r, strArg_p, strArg_l, strArgErr
    Dim strReportFileName, strPrintFileName, intPrintFileLinesPerPage, txtSelectClause

    On Error Resume Next

    Set objArgs = WScript.Arguments
    Set crApp = WScript.CreateObject ("CrystalRuntime.Application")

    If (Err.Number <> 0) Then
       WScript.Echo "ERROR: " & Err.Number & " - " & Err.Description
    End If

    ' Declare your variables here (when run with no command-line switches)
    ' These are always superceded by command-line arguments.

    dateDate = Date()
    dateTime = Time()
    strRunDate = Right("0" & Day(dateDate), 2) & " " & MonthName(Month(dateDate), True) & " " & Year(dateDate)
    strRunTime = Hour(dateTime) & ":" & Right("0" & Minute(dateTime), 2) & ":" & Right("0" & Second(dateTime), 2)

    ' End of user variables

    For intI = 0 to objArgs.Count - 1
       strArgs = strArgs & " " & objArgs(intI)

    If (intI > 0) Then
       strArgs = Right(strArgs,Len(strArgs)-1)
       WScript.Echo "Arguments on command line: [" & strArgs & "]"
       WScript.Echo ""
       WScript.Echo "Arguments on command line: []"
       WScript.Echo ""
    End If

    If (objArgs.Count > 0) Then
       For intI = 0 to objArgs.Count - 1
          If (InStr(1,objArgs(intI),"-r:")) Then
             strParseArray = split(objArgs(intI),"-r:")
             strReportFileName = strParseArray(1)
             strArg_r = True
          ElseIf (InStr(1,objArgs(intI),"-p:")) Then
             strParseArray = split(objArgs(intI),"-p:")
             strPrintFileName = strParseArray(1)
             strArg_p = True
          ElseIf (InStr(1,objArgs(intI),"-l:")) Then
             strParseArray = split(objArgs(intI),"-l:")
             intPrintFileLinesPerPage = strParseArray(1)
             strArg_l = True
             strArgErr = True
          End If
    End If

    If (strArg_r <> True or strArg_p <> True or strArg_l <> True or strArgErr = True) Then
       WScript.Echo "Usage: biCRExport.vbs [ -r:<ReportFileName> ] [ -p:<PrintFileName> ] [ -l:<PrintFileLinesPerPage> ]"
       WScript.Echo ""
       WScript.Echo "r <ReportFileName> - required, the filename of the report to be printed."
       WScript.Echo "p <PrintFileName> - required, the filename for output when printing to file."
       WScript.Echo "l <PrintFileLinesPerPage> - required, lines per page for paginated text export."
    End If


    Set crRpt = crApp.OpenReport(strReportFileName)

    crRpt.ExportOptions.DestinationType = 1 'crEDTDiskFile
    crRpt.ExportOptions.DiskFileName = strPrintFileName 'The filename for output when printing to file.
    crRpt.ExportOptions.FormatType = 10 'crEFTPaginatedText 'The type of file created when printing to file.
    crRpt.ExportOptions.NumberOfLinesPerPage = intPrintFileLinesPerPage 'Lines per page for paginated text export.

    If (Err.Number = 0) Then
       crRpt.Export False 'Set this property to 1 to print the report.
    End If

    If (Err.Number <> 0) Then
       WScript.Echo "ERROR: " & Err.Number & " - " & Err.Description
       WScript.Echo ""
    End If

    Set objArgs = Nothing
    Set crRpt = Nothing

    WScript.Echo "FINISH: " & Date & " " & Time


    Function ConfirmFileExists(strFileName)

       'PURPOSE : Confirm that a file exists.
       'REQUIRES: The file name including the full path.
       'PROMISE : Returns a boolean True if the file exists.

       Dim objFileSystem

       Set objFileSystem = WScript.CreateObject("Scripting.FileSystemObject")

       If (objFileSystem.FileExists(strFileName)) Then
          ConfirmFileExists = True
          WScript.Echo "ERROR: File does not exist: " & strFileName
       End If

       Set objFileSystem = Nothing

    End Function

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    Hot fix for .Net Crystal Reports 10.2.3600.0 to fix problems with sub reports running on 64 bit operating systems ISSUE: Reports which contain subreports fail with error "Missing Parameter Value" DEPLOYMENT SERVER OS: Windows 2008 with 64 bi…
    Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
    This video discusses moving either the default database or any database to a new volume.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now