export subreport to excel (1 Viewer)

Jaye7

Registered User.
Local time
Tomorrow, 06:36
Joined
Aug 19, 2014
Messages
205
I have used the following script to export a subform to excel but it isn't working for a report.

The report name is spelled correctly etc... but the error message keeps saying error 2465 or if I enable the error handler it says application defined or object defined error.

I don't want to have to export the data to a table first, but if I have to then I guess I will have to.

Code:
Sub fFunctionNameExportExcel(ctrl As IRibbonControl)
'print to excel
Dim rst As DAO.Recordset
    Dim ApXL As Object
    Dim xlWBk As Object
    Dim xlWSh As Object
    Dim fld As DAO.Field
    Const xlCenter As Long = -4108
    Const xlBottom As Long = -4107
    'On Error GoTo err_handler
   [B][COLOR=Red] Set rst = Reports!StockTakeReport!StockTakeReportList.Report.RecordsetClone '[/COLOR][/B] name of your form and subform or report
    Set ApXL = CreateObject("Excel.Application")
    Set xlWBk = ApXL.Workbooks.Add
    ApXL.Visible = True
    
    ApXL.screenupdating = False
        
    Set xlWSh = xlWBk.Worksheets("Sheet1")
    If Len(strSheetName) > 0 Then
        xlWSh.Name = Left(strSheetName, 34)
    End If
    xlWSh.Activate
    xlWSh.Range("A1").Select
 
 
    For Each fld In rst.Fields
        ApXL.ActiveCell = fld.Name
        ApXL.ActiveCell.Offset(0, 1).Select
    Next
    rst.MoveFirst
    xlWSh.Range("A2").CopyFromRecordset rst
    xlWSh.Range("1:1").Select
    ' This is included to show some of what you can do about formatting.
    'You can comment out or delete
    ' any of this below that you don't want to
    'use in your own export.
    With ApXL.Selection.Font
        .Name = "Arial"
        .Size = 12
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
    End With
    ApXL.Selection.Font.Bold = True
    With ApXL.Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .MergeCells = False
    End With
    ' selects all of the cells
    ApXL.ActiveSheet.Cells.Select
    ' does the "autofit" for all columns
    'ApXL.ActiveSheet.Cells.EntireColumn.AutoFit
    ' selects the first cell to unselect all cells
    
    
    xlWSh.Rows("2:2").Select
    'freeze pane
    ApXL.ActiveWindow.FreezePanes = True
    
    xlWSh.Range("A1").Select
 
    rst.Close
    Set rst = Nothing
    
    ApXL.screenupdating = True
    Exit Sub
err_handler:
    DoCmd.SetWarnings True
    MsgBox Err.Description, vbExclamation, Err.Number
    Exit Sub
 

JHB

Have been here a while
Local time
Today, 22:36
Joined
Jun 17, 2012
Messages
7,732
Try to add the CurrentDb in front, (not tested).
Code:
  [B] Set rst = [/B][B][COLOR=Red]CurrentDb.[/COLOR][/B][COLOR=Red][COLOR=Black]Reports!StockTakeReport!StockTakeReportList.Report.RecordsetClone [/COLOR][/COLOR]
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 06:36
Joined
Jan 20, 2009
Messages
12,852
Try to add the CurrentDb in front, (not tested).
Code:
  [B] Set rst = [/B][B][COLOR=red]CurrentDb.[/COLOR][/B][COLOR=red][COLOR=black]Reports!StockTakeReport!StockTakeReportList.Report.RecordsetClone [/COLOR][/COLOR]

The Reports Collection is a member of Application. It need not be specified since Application is the default.

The problem is that, unlike a form's recordset, a report's recordset is not available to VBA.
 

Users who are viewing this thread

Top Bottom