[SIZE=2]Public Function SendTQ2XLWbSheet(strTQName As String, strSheetName As String, strFilePath As String)[/SIZE]
[SIZE=2]' strTQName is the name of the table or query you want to send to Excel[/SIZE]
[SIZE=2]' strSheetName is the name of the sheet you want to send it to[/SIZE]
[SIZE=2]' strFilePath is the name and path of the file you want to send this data into.[/SIZE]
[SIZE=2] Dim rst As DAO.Recordset[/SIZE]
[SIZE=2] Dim xlWBk As Object[/SIZE]
[SIZE=2] Dim xlWSh As Object[/SIZE]
[SIZE=2] Dim fld As DAO.Field[/SIZE]
[SIZE=2] Dim strPath As String[/SIZE]
[SIZE=2] Const xlCenter As Long = -4108[/SIZE]
[SIZE=2] Const xlBottom As Long = -4107[/SIZE]
[SIZE=2] On Error GoTo err_handler[/SIZE]
[SIZE=2] strPath = strFilePath[/SIZE]
[SIZE=2] Set rst = CurrentDb.OpenRecordset(strTQName)[/SIZE]
[SIZE=2][COLOR=red][B][SIZE=2]If ApXL Is Nothing Then[/SIZE][/B][/COLOR]
[SIZE=2][COLOR=red][B] Set ApXL = CreateObject("Excel.Application")[/B][/COLOR][/SIZE]
[COLOR=red][B][SIZE=2] End If[/SIZE][/B][/COLOR]
[/SIZE]
[SIZE=2] Set xlWBk = ApXL.Workbooks.Open(strPath)[/SIZE]
[SIZE=2] ApXL.Visible = True[/SIZE]
[SIZE=2] Set xlWSh = xlWBk.Worksheets(strSheetName)[/SIZE]
[SIZE=2] xlWSh.Range("A1").Select[/SIZE]
[SIZE=2] For Each fld In rst.Fields[/SIZE]
[SIZE=2] ApXL.ActiveCell = fld.Name[/SIZE]
[SIZE=2] ApXL.ActiveCell.Offset(0, 1).Select[/SIZE]
[SIZE=2] Next[/SIZE]
[SIZE=2] rst.MoveFirst[/SIZE]
[SIZE=2] xlWSh.Range("A2").CopyFromRecordset rst[/SIZE]
[SIZE=2] xlWSh.Range("1:1").Select[/SIZE]
[SIZE=2] ' This is included to show some of what you can do about formatting. You can comment out or delete[/SIZE]
[SIZE=2] ' any of this that you don't want to use in your own export.[/SIZE]
[SIZE=2] With ApXL.Selection.Font[/SIZE]
[SIZE=2] .Name = "Arial"[/SIZE]
[SIZE=2] .Size = 12[/SIZE]
[SIZE=2] .Strikethrough = False[/SIZE]
[SIZE=2] .Superscript = False[/SIZE]
[SIZE=2] .Subscript = False[/SIZE]
[SIZE=2] .OutlineFont = False[/SIZE]
[SIZE=2] .Shadow = False[/SIZE]
[SIZE=2] End With[/SIZE]
[SIZE=2] ApXL.Selection.Font.Bold = True[/SIZE]
[SIZE=2] With ApXL.Selection[/SIZE]
[SIZE=2] .HorizontalAlignment = xlCenter[/SIZE]
[SIZE=2] .VerticalAlignment = xlBottom[/SIZE]
[SIZE=2] .WrapText = False[/SIZE]
[SIZE=2] .Orientation = 0[/SIZE]
[SIZE=2] .AddIndent = False[/SIZE]
[SIZE=2] .IndentLevel = 0[/SIZE]
[SIZE=2] .ShrinkToFit = False[/SIZE]
[SIZE=2] .MergeCells = False[/SIZE]
[SIZE=2] End With[/SIZE]
[SIZE=2] ' selects all of the cells[/SIZE]
[SIZE=2] ApXL.ActiveSheet.Cells.Select[/SIZE]
[SIZE=2] ' does the "autofit" for all columns[/SIZE]
[SIZE=2] ApXL.ActiveSheet.Cells.EntireColumn.AutoFit[/SIZE]
[SIZE=2] ' selects the first cell to unselect all cells[/SIZE]
[SIZE=2] xlWSh.Range("A1").Select[/SIZE]
[SIZE=2] [B][COLOR=red]xlW[/COLOR][COLOR=red]B.Close[/COLOR][/B][/SIZE] [B][COLOR=#ff0000]True[/COLOR][/B]
[SIZE=2] [/SIZE]
[SIZE=2][COLOR=red][COLOR=black] rst.Close[/COLOR][/COLOR][/SIZE]
[SIZE=2] Set rst = Nothing[/SIZE]
[SIZE=2]Exit_SendTQ2XLWbSheet: [/SIZE]
[SIZE=2] Exit Function[/SIZE]
[SIZE=2]err_handler:[/SIZE]
[SIZE=2] DoCmd.SetWarnings True[/SIZE]
[SIZE=2] MsgBox Err.Description, vbExclamation, Err.Number[/SIZE]
[SIZE=2] Resume Exit_SendTQ2XLWbSheet [/SIZE]
[SIZE=2]End Function[/SIZE]