Private Sub cmdExpToExcel_Click()
'code behind command button "Export to Excel"
Dim lngMax As Long
Dim lngCount As Long
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Dim strFile As String
Dim running As Boolean
Set maindb = CurrentDb()
Set mainqdf = maindb.QueryDefs("qryCOSearch")
Set mainRst = mainqdf.OpenRecordset(dbOpenDynaset, dbEdit)
'all code below explains exporting the query results to excel
'allow user to choose path to save to
strFile = GetSaveFile_CLT("C:\", "Save this file as",
"strDefName")
If strFile = "" Then
'user clicked cancel
Exit Sub
End If
'defining the variables
On Error Resume Next
Set xlApp = GetObject("Excel.Application")
If xlApp Is Nothing Then
Set xlApp = CreateObject("Excel.Application")
Err.Clear
End If
On Error GoTo Err_Handler
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets.Add
'formatting cells in excel
With xlSheet
For Each Cell In xlSheet.Range("A1", "S1")
Cell.Font.Size = 10
Cell.Font.Name = "Arial"
Cell.Font.Bold = True
Cell.Interior.Color = rgb(204, 255, 255)
Cell.HorizontalAlignment = xlHAlignCenter
Cell.WrapText = True
Next
.Cells(1, 2).HorizontalAlignment = xlHAlignLeft
.Columns("A:S").HorizontalAlignment = xlHAlignLeft
.Columns("A").ColumnWidth = 10
.Columns("B").ColumnWidth = 24
.Columns("C:D").ColumnWidth = 12
.Columns("E").ColumnWidth = 40
.Columns("F").ColumnWidth = 30
.Columns("G").ColumnWidth = 8
.Columns("H").ColumnWidth = 32
.Columns("I:J").ColumnWidth = 24
.Rows(1).RowHeight = 16
End With
'deleting all other worksheets except for "Results"
For lngCount = lngMax To 1 Step -1
If xlBook.Worksheets(lngCount).Name <> "Results" Then
xlBook.Worksheets(lngCount).Delete
End If
Next lngCount
'copying the query results from the recordset to the
excel file
With xlSheet
.Name = "Results"
.UsedRange.ClearContents
lngMax = mainRst.Fields.Count
For lngCount = lngMax To 1 Step -1
.Cells(1, lngCount).Value = mainRst.Fields(lngCount -
1).Name
Next lngCount
.Range("A2").CopyFromRecordset mainRst
End With
lngMax = xlBook.Worksheets.Count
'deleting all other worksheets except for "Results"
For lngCount = lngMax To 1 Step -1
If xlBook.Worksheets(lngCount).Name <> "Results" Then
xlBook.Worksheets(lngCount).Delete
End If
Next lngCount
xlBook.SaveAs strFile
MsgBox "Export Completed,Do you want to open your
file?", vbYesNo
If Yes Then
xlApp.Visible = True
xlApp.UserControl = True
xlBook.Visible = True
xlBook.UserControl = True
End If
If No Then
'stay in result window, clean up by closing objects
and ending excel process
xlApp.Visible = False
xlBook.Visible = False
xlSheet.Close True
xlBook.Close True
xlApp.Close True
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
xlApp.Quit = True
End If
Exit_Handler:
'clean up
If Not mainRst Is Nothing Then
mainRst.Close
Set mainRst = Nothing
End If
If Not mainqdf Is Nothing Then
Set mainqdf = Nothing
End If
If Not maindb Is Nothing Then
Set maindb = Nothing
End If
Exit Sub
Err_Handler:
On Error Resume Next
Resume Exit_Handler
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume
End Sub