Hello Joe,
Try this code and let me know if you want more.
grtz
Function createExcelDocument()
Dim db As Database
Dim rs As Recordset
Dim xlApp As Object 'Excel.Application
Dim xlBook As Object 'Excel.Workbook
Dim xlSheet As Object 'Excel.Worksheet
Dim xlRange As Object 'Excel.Range
Dim xlPrintRange As Object 'Excel.Range
Dim xlPageSetup As Object 'Excel.PageSetup
Dim I, J As Integer
'!<-- constants can be placed in a module as Global Const xlSolid = 1
etc....
Const xlSolid = 1
Const xlThin = 2
Const xlNone = -4142
Const xlDiagonalDown = 5
Const xlDiagonalUp = 6
Const xlEdgeLeft = 7
Const xlEdgeTop = 8
Const xlEdgeBottom = 9
Const xlEdgeRight = 10
Const xlContinuous = 1
Const xlAutomatic = -4105
Const xlDown = -4142
Const xlToLeft = -4159
Const xlPrintNoComments = -4142
Const xlPaperA4 = 9
Const xlDownThenOver = 1
'-->
On Error GoTo Error_createExcelDocument
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT * FROM MSysObjects") 'Put your one query
here
rs.MoveLast
rs.MoveFirst
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add()
Set xlSheet = xlBook.Worksheets(1)
'Maak eerste rij geel
Set xlRange = xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(1, 29))
With xlRange.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
With xlRange.Borders(xlDiagonalDown)
.LineStyle = xlNone
End With
With xlRange.Borders(xlDiagonalUp)
.LineStyle = xlNone
End With
With xlRange.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlRange.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlRange.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlRange.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
xlSheet.Cells(2, 1).Select
xlApp.ActiveWindow.FreezePanes = True
xlSheet.Cells(1, 1).Select
xlApp.Visible = True
For J = 0 To rs.Fields.Count - 1
xlSheet.Cells(1, J + 1) = rs.Fields(J).Name
Next J
For I = 1 To rs.RecordCount
For J = 0 To rs.Fields.Count - 1
'In this example only to skip binary fields
If rs.Fields(J).Type <> 11 Then
xlSheet.Cells(I + 1, J + 1) = rs.Fields(J).Value
End If
Next J
rs.MoveNext
Next I
'or
'xlSheet.Cells(1, 1).CopyFromRecordset rs ' is not always working
Exit Function
Error_createExcelDocument:
If Err = 3021 Then
MsgBox ("No records !"), vbCritical
ElseIf Err = 53 Then
Resume Next
Else
MsgBox ("Error " & Err & "(" & Err.Description & ") has occurred in
procedure <createExcelDocument> !"), vbCritical
xlApp.Quit
End If
DoCmd.Hourglass False
End Function