VBA экспорт файла в Excel

Дата: 05.03.2008 14:09:53
Sub ExportFile()

Dim db As DAO.Database
Dim rst As DAO.Recordset

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Dim i As Integer
Dim filename As String

On Error GoTo HandleErr

Set xlApp = New Excel.Application

Set xlBook = New Excel.Workbook
Set xlSheet = xlBook.ActiveSheet

Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT Урожай FROM Урожайность;")

If rst.EOF And rst.BOF Then
MsgBox "Ошибка!"
GoTo ExitHere
End If

With xlSheet

For i = 0 To rst.Fields.Count - 1
With .Cells(5, 1 + i)
.Font.Size = 8
.Value = rst.Fields(i).Name
.Font.Bold = True
End With
.Range("A6").CopyFromRecordset rst
End With


On Error Resume Next

Set rst = Nothing
Set db = Nothing
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing

MsgBox Err & ":" & Err.Description, , "Îøèáêà"
Resume ExitHere
Resume Next

End sub