How to open a Crystall Report XI with VBScript

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?
edmondwuAsked:
Who is Participating?
 
edmondwuAuthor Commented:
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
   WScript.Quit(1)
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)
Next

If (intI > 0) Then
   strArgs = Right(strArgs,Len(strArgs)-1)
   WScript.Echo "Arguments on command line: [" & strArgs & "]"
   WScript.Echo ""
Else
   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
      Else
         strArgErr = True
      End If
   Next
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."
   WScript.Quit(1)
End If

ConfirmFileExists(strReportFileName)

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

WScript.Quit(Err.Number)

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
   Else
      WScript.Echo "ERROR: File does not exist: " & strFileName
      WScript.Quit(1)
   End If

   Set objFileSystem = Nothing

End Function
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.