[FONT=courier new]Public Function SendTQ2Excel(strTQName As String, Optional strSheetName As String[COLOR=red], Optional strFileName As String[/COLOR])[/FONT]
[FONT=courier new]' strTQName is the name of the table or query you want to send to Excel[/FONT]
[FONT=courier new]' strSheetName is the name of the sheet you want to name it to[/FONT]
[FONT=courier new] Dim rst As DAO.Recordset[/FONT]
[FONT=courier new] Dim ApXL As Object[/FONT]
[FONT=courier new] Dim xlWBk As Object[/FONT]
[FONT=courier new] Dim xlWSh As Object[/FONT]
[FONT=courier new] Dim fld As Field[/FONT]
[FONT=courier new] Const xlCenter As Long = -4108[/FONT]
[FONT=courier new] Const xlBottom As Long = -4107[/FONT]
[FONT=courier new] On Error GoTo err_handler[/FONT]
[FONT=courier new] Set rst = CurrentDb.OpenRecordset(strTQName)[/FONT]
[FONT=courier new] Set ApXL = CreateObject("Excel.Application")[/FONT]
[FONT=courier new] Set xlWBk = ApXL.Workbooks.Add[/FONT]
[FONT=courier new] ApXL.Visible = True[/FONT]
[FONT=courier new] Set xlWSh = xlWBk.Worksheets("Sheet1")[/FONT]
[FONT=courier new] If Len(strSheetName) > 0 Then[/FONT]
[FONT=courier new] xlWSh.Name = Left(strSheetName, 34)[/FONT]
[FONT=courier new] End If[/FONT]
[FONT=courier new] xlWSh.Range("A1").Select[/FONT]
[FONT=courier new] For Each fld In rst.Fields[/FONT]
[FONT=courier new] ApXL.ActiveCell = fld.Name[/FONT]
[FONT=courier new] ApXL.ActiveCell.Offset(0, 1).Select[/FONT]
[FONT=courier new] Next[/FONT]
[FONT=courier new] rst.MoveFirst[/FONT]
[FONT=courier new] xlWSh.Range("A2").CopyFromRecordset rst[/FONT]
[FONT=courier new] xlWSh.Range("1:1").Select[/FONT]
[FONT=courier new] ' This is included to show some of what you can do about formatting. You can comment out or delete[/FONT]
[FONT=courier new] ' any of this that you don't want to use in your own export.[/FONT]
[FONT=courier new] With ApXL.Selection.Font[/FONT]
[FONT=courier new] .Name = "Arial"[/FONT]
[FONT=courier new] .Size = 12[/FONT]
[FONT=courier new] .Strikethrough = False[/FONT]
[FONT=courier new] .Superscript = False[/FONT]
[FONT=courier new] .Subscript = False[/FONT]
[FONT=courier new] .OutlineFont = False[/FONT]
[FONT=courier new] .Shadow = False[/FONT]
[FONT=courier new] End With[/FONT]
[FONT=courier new] ApXL.Selection.Font.Bold = True[/FONT]
[FONT=courier new] With ApXL.Selection[/FONT]
[FONT=courier new] .HorizontalAlignment = xlCenter[/FONT]
[FONT=courier new] .VerticalAlignment = xlBottom[/FONT]
[FONT=courier new] .WrapText = False[/FONT]
[FONT=courier new] .Orientation = 0[/FONT]
[FONT=courier new] .AddIndent = False[/FONT]
[FONT=courier new] .IndentLevel = 0[/FONT]
[FONT=courier new] .ShrinkToFit = False[/FONT]
[FONT=courier new] .MergeCells = False[/FONT]
[FONT=courier new] End With[/FONT]
[FONT=courier new] ' selects all of the cells[/FONT]
[FONT=courier new] ApXL.ActiveSheet.Cells.Select[/FONT]
[FONT=courier new] ' does the "autofit" for all columns[/FONT]
[FONT=courier new] ApXL.ActiveSheet.Cells.EntireColumn.AutoFit[/FONT]
[FONT=courier new] ' selects the first cell to unselect all cells[/FONT]
[FONT=courier new] xlWSh.Range("A1").Select[/FONT]
[FONT=courier new][COLOR=seagreen] ' If there is a file name, then we save and close it[/COLOR][/FONT]
[FONT=courier new][COLOR=red] If strFileName <> "" Then[/COLOR][/FONT]
[FONT=courier new][COLOR=red] xlWBk.SaveAs strFileName[/COLOR][/FONT]
[FONT=courier new][COLOR=red] ApXL.Quit[/COLOR][/FONT]
[FONT=courier new][COLOR=red] Set ApXL = Nothing[/COLOR][/FONT]
[FONT=courier new][COLOR=red] End If[/COLOR][/FONT]
[FONT=courier new] rst.Close[/FONT]
[FONT=courier new] Set rst = Nothing[/FONT]
[FONT=courier new] Exit Function[/FONT]
[FONT=courier new]err_handler:[/FONT]
[FONT=courier new] DoCmd.SetWarnings True[/FONT]
[FONT=courier new] MsgBox Err.Description, vbExclamation, Err.Number[/FONT]
[FONT=courier new] Exit Function[/FONT]
[FONT=courier new]End Function[/FONT]