TheVillageIdiot27
Programmer
I am trying to write a simple command line tool in VB to export crystal reports to pdfs.
It's a bit of work in progress but I can't for the life of me seem to get parameters to work.
an example of the command line syntax is
where theparameter is the crystal parameter name and "Billy" is the value.
I am using crystal version 12.
I am sure this must be quite simple but I am copying the code I am finding on the internet, but I can't seem to get it to work.
FYI - For simplicities sake I am only trying to get strings to work at the moment.
If anyone can see what I am doing wrong I would really appreciate the assistance.
It's a bit of work in progress but I can't for the life of me seem to get parameters to work.
an example of the command line syntax is
Code:
"C:\runme.exe" /input="c:\myreport.rpt" /output="C:\test.pdf" /bookmarks="yes" /theparameter="Billy"
where theparameter is the crystal parameter name and "Billy" is the value.
I am using crystal version 12.
Code:
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Module modMain
Sub Main(ByVal Params() As String)
Dim strInputReport As String
Dim strOutputPDF As String
Dim strBookMarks As String
Dim blnBookMarks As Boolean
Dim strResponse As String
Dim objCrReport As New ReportDocument
Dim objCrExportOptions As ExportOptions
Dim objCrDiskFileDestinationOptions As New DiskFileDestinationOptions()
Dim objCrPDFFormatOptions As New PdfFormatOptions
Dim objCrParameterDiscreteValue As ParameterDiscreteValue
Dim objCrParameterFieldDefinitions As ParameterFieldDefinitions
Dim objCrParameterFieldDefinition As ParameterFieldDefinition
Dim objCrParameterValues As ParameterValues
Dim strCrParameterName
Dim strCrParameterValue
Dim varParams = Split(Microsoft.VisualBasic.Command, " /")
Dim strParam
Dim strParamName As String
Dim strParamValue As String
Dim objParams As New Collection
Dim objAdditionalParams As New Collection
Dim objLogFile As System.IO.StreamWriter
' Log File
objLogFile = IO.File.AppendText(System.AppDomain.CurrentDomain.BaseDirectory & "\log.txt")
objLogFile.WriteLine("______________________________________________________________________")
' Load Input Parameters Into Paramaters Collection
For Each strParam In varParams
strParamName = Replace(Split(strParam, "=")(0), "/", "")
strParamValue = Replace(Split(strParam, "=")(1), """", "")
objParams.Add(strParamValue, strParamName)
' Additional Input Parameters - Assume These Are Report Parameters Store Names For Later
If strParamName <> "input" And strParamName <> "output" And strParamName <> "bookmarks" Then
objAdditionalParams.Add(strParamName)
End If
Next
' Get Input String
Try
strInputReport = objParams("input")
Catch err As Exception
strResponse = "Error - Input Not Declared"
System.Console.WriteLine(strResponse)
objLogFile.WriteLine(strResponse)
objLogFile.Close()
Exit Sub
End Try
' Get Output String
Try
strOutputPDF = objParams("output")
Catch err As Exception
strResponse = "Error - Output Not Declared"
System.Console.WriteLine(strResponse)
objLogFile.WriteLine(strResponse)
objLogFile.Close()
Exit Sub
End Try
' Get Bookmarks String
Try
strBookMarks = objParams("bookmarks")
If strBookMarks = "yes" Then
blnBookMarks = True
Else
blnBookMarks = False
End If
Catch err As Exception
blnBookMarks = False
End Try
' Load Report
strResponse = "Loading Report " & strInputReport
System.Console.WriteLine(strResponse)
objLogFile.WriteLine(strResponse)
With objCrReport
.Load(strInputReport)
End With
' Parameters
objCrParameterFieldDefinitions = objCrReport.DataDefinition.ParameterFields
For Each strCrParameterName In objAdditionalParams
Try
strResponse = "Setting Parameter " & strCrParameterName & " As " & objParams(strCrParameterName) & " In " & strInputReport
System.Console.WriteLine(strResponse)
objLogFile.WriteLine(strResponse)
objCrParameterFieldDefinition = objCrParameterFieldDefinitions.Item(strCrParameterName)
strCrParameterValue = objParams(strCrParameterName)
objCrParameterValues = objCrParameterFieldDefinition.CurrentValues
objCrParameterDiscreteValue = New ParameterDiscreteValue
objCrParameterDiscreteValue.Value = strCrParameterValue
objCrParameterValues.Add(objCrParameterDiscreteValue)
'Errors Here
objCrParameterFieldDefinition.ApplyCurrentValues(objCrParameterDiscreteValue)
Catch Err As Exception
strResponse = "Error Setting Parameter " & strCrParameterName & " in " & strInputReport
System.Console.WriteLine(strResponse)
objLogFile.WriteLine(strResponse)
End Try
Next
strResponse = "Formating Report " & strInputReport
System.Console.WriteLine(strResponse)
objLogFile.WriteLine(strResponse)
' Set the destination path and file name
objCrDiskFileDestinationOptions.DiskFileName = strOutputPDF
' Format Options
With objCrPDFFormatOptions
.CreateBookmarksFromGroupTree = blnBookMarks
End With
' Set Export Options
objCrExportOptions = objCrReport.ExportOptions
With objCrExportOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.PortableDocFormat
.DestinationOptions = objCrDiskFileDestinationOptions
.FormatOptions = objCrPDFFormatOptions
End With
' Export The Report
strResponse = "Exporting Report To " & strOutputPDF
System.Console.WriteLine(strResponse)
objLogFile.WriteLine(strResponse)
Try
objCrReport.Refresh()
objCrReport.Export()
objLogFile.Close()
Catch err As Exception
strResponse = "Error Exporting " & strInputReport & ": " & err.ToString
System.Console.WriteLine(strResponse)
objLogFile.WriteLine(strResponse)
objLogFile.Close()
End Try
End Sub
End Module
I am sure this must be quite simple but I am copying the code I am finding on the internet, but I can't seem to get it to work.
FYI - For simplicities sake I am only trying to get strings to work at the moment.
If anyone can see what I am doing wrong I would really appreciate the assistance.