VB6+Crystal Report 8.0预览后导出Excel报文件找不到错误求助
I've run into this exact conflict between the CRViewer control (your Crt1 object) and the CRAXDRT automation library before with CR8.0. The issue stems from resource/connection conflicts between the two Crystal Reports object models—the CRViewer doesn't fully release database resources after preview, which messes up the CRAXDRT instance's ability to resolve table locations when you try to export next. Here are the fixes that worked for me, tailored to your 300+ report setup (no version upgrades needed):
Fix 1: Clean Up CRViewer Resources After Preview
Modify your existing Preview sub to explicitly reset the CRViewer control once the preview window is closed. This ensures it doesn't hold onto stale database connections or report references:
Private Sub Preview() Crt1.ReportFileName = "C:\Sample_Report.rpt" Crt1.SelectionFormula = "" Crt1.DiscardSavedData = True Crt1.WindowState = crptMaximized Crt1.Action = 1 ' Reset the control after preview closes to release resources Crt1.ReportFileName = "" Set Crt1.Report = Nothing End Sub
Fix 2: Strengthen Export Code with Explicit Connection Details
Your export code was missing critical connection configuration for Access MDBs. CR8.0 needs explicit OLE DB provider details to avoid confusion after preview. Update your Report_ExportToExcel sub to:
- Use the passed
ReportPathinstead of hardcoding (better for your 300+ reports) - Explicitly set the OLE DB provider and connection string for each table
- Always create a fresh
CRAXDRT.Applicationinstance per export
Private Sub Report_ExportToExcel(ReportPath As String) On Error GoTo Loc Dim path1 As String path1 = Save_Dialog ' Open save dialog to get output path If path1 = "" Then Exit Sub ' Create a fresh CRAXDRT instance to avoid conflicts with CRViewer Dim appl As New CRAXDRT.Application Dim dbTable As CRAXDRT.DatabaseTable Dim rep As CRAXDRT.Report Set rep = appl.OpenReport(ReportPath, 1) rep.DiscardSavedData ' Update each table's connection explicitly (critical for Access) For Each dbTable In rep.Database.Tables ' Set logon info: empty username/password for Access, specify MDB path dbTable.SetLogOnInfo "", "", "C:\Sample.mdb", "" dbTable.Location = "C:\Sample.mdb" ' Force the correct Jet OLE DB provider dbTable.Connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Sample.mdb;Persist Security Info=False" Next ' Configure export settings With rep.ExportOptions .DiskFileName = path1 .DestinationType = crEDTDiskFile .ExcelAreaType = crDetail .ExcelUseTabularFormat = True .ExcelTabHasColumnHeadings = True .FormatType = crEFTExcel80Tabular End With rep.Export False ' Clean up export resources Set rep = Nothing Set appl = Nothing Exit Sub Loc: MsgBox Err.Description ' Ensure cleanup even on error Set rep = Nothing Set appl = Nothing End Sub
Fix 3: Force CRViewer to Close Before Export (If Needed)
If the first two fixes don't resolve the issue, add a check to close any active CRViewer preview before starting the export. Add this code right before calling Report_ExportToExcel:
' Ensure CRViewer is not holding a report before exporting If Not Crt1.Report Is Nothing Then Crt1.Close Set Crt1.Report = Nothing Crt1.ReportFileName = "" End If ' Now call your export function Report_ExportToExcel "C:\Sample_Report.rpt"
Why This Works
CRViewer (used for preview) and CRAXDRT (used for export) share underlying CR8.0 runtime components but run in separate contexts. When you preview a report, CRViewer caches database connection details that can interfere with subsequent CRAXDRT operations. By cleaning up CRViewer resources, using fresh CRAXDRT instances, and explicitly defining connection parameters, you eliminate these conflicts entirely—no need to upgrade your 300+ reports.
内容的提问来源于stack exchange,提问作者RAJ

