Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print Crystal Report to Disk from VB.NET 1

Status
Not open for further replies.

johnhugh

Technical User
Mar 24, 2010
702
SG
Hi, I'm very new to VB.NET and am in need for some help.
For 2 days I'm now trying to get my code to print a PDF from Crystal to PDF.

I'm trying to translate a VB6 program to VB.Net 2010.
This is the VB6 program. All it does is, take an existing report and print a pdf to disk.
The only reason I want to migrate it to VB.NET is because in VB6 I don't seem to be able to export Group Headers as PDF bookmarks.
If anyone know a way of exporting group headers as bookmarks I can save me the whole .net translation.

Code:
Dim crystal As CRAXDRT.Application
Dim report As CRAXDRT.report       
Set crystal = CreateObject("CrystalRuntime.Application.10")
Set report = crystal.OpenReport("c:\Balances.rpt")

report.EnableParameterPrompting = False
Set Parameters = report.ParameterFields
For cnt = 1 To Parameters.Count
  Set Parameters = Parameters.Item(cnt)
  If Parameters.Name = "{?Country}" Then
    Parameters.ClearCurrentValueAndRange
    Parameters.AddCurrentValue "10" 'Tanzania
  End If
Next

'report.ExportOptions.CreateBookmarksFromGroupTree = True
report.ExportOptions.DiskFileName = "c:\Balances.pdf"
report.ExportOptions.FormatType = crEFTPortableDocFormat
report.ExportOptions.DestinationType = crEDTDiskFile
report.ExportOptions.PDFExportAllPages = True
report.Export False

Set report = Nothing
Set crystal = Nothing

I've started in VB.Net already but can simply not get it to work.
I'm testing with CR XI R2 and Visual Studio 2010.

I start by creating a new Crystal Reports Application and choose the report I want to use. I figure this might not be necessary as I don't want anything to display, just run the .exe to print the pdf as an scheduled process later on.
I've removed the CrystalReportViewer and the CrystalReportDocument control from my form and just added a button to test it.

This is my code:

Code:
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Windows.Forms

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim crReport As ReportDocument
            Dim crPDFOptions As PdfFormatOptions

            Dim strFileName As String
            Dim reportPath As String = ""

            strFileName = "test"
            crReport = New CrystalDecisions.CrystalReports.Engine.ReportDocument '[COLOR=red] here it throws an exception [/color]

            crReport.Load("c:\Balances.rpt", OpenReportMethod.OpenReportByTempCopy)

            crPDFOptions = New PdfFormatOptions()
            crPDFOptions.CreateBookmarksFromGroupTree = True

            Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
            Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
            CrDiskFileDestinationOptions.DiskFileName = "C:\" & strFileName & ".pdf"
            With crReport.ExportOptions
                .ExportDestinationType = ExportDestinationType.DiskFile
                .ExportFormatType = ExportFormatType.PortableDocFormat
                .DestinationOptions = CrDiskFileDestinationOptions
                .FormatOptions = CrFormatTypeOptions
            End With
            crReport.Export()

            crReport = Nothing
            CrDiskFileDestinationOptions = Nothing
            CrFormatTypeOptions = Nothing
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

The exception it throws at crReport = New CrystalDecisions.CrystalReports.Engine.ReportDocument is the following.

"Retrieving the COM class factory for component with CLSID {4DB2E2BB-78E6-4AEA-BEFB-FDAAB610FD1B} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))."

I don't have any references to a COM class and don't hink I should have.
But this is about all I can read from that error.

Any help is really appreciated.

 
Is your PC a 64-bit machine? If it is, try changing the Target CPU to x86:


Project menu -> <ProjectName> Properties

In the Properties window, click on "Compile" in the left pane. In the right pane, click the "Advanced Compile Options" button. In the "Advanced Compiler Settings" dialog, change the "Target CPU" dropdown to "x86". Hit OK and run the application.

You might also try installing the Crystal Reports runtime files, which can be found through Google.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Setting the target CPU fixed it. Thank you so much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top