Hi
I am currently using this code i got from SI_the_Geek from an old post to export data from Access to Excel. I would like to also export the headings and formatting from Access into Excel just like it is possible when doing it manually from inside of Access. has anyone ever done this? I suppose it could be done by writing the code for each field heading, but i wonder if there is an easier way.
I also get an error message "The file you are trying to open is in a different format than specified by the file extension" when i try to open the excel sheet.
How can i open the excel sheet after it has been filled?
Thanks in advance for your help.
I am currently using this code i got from SI_the_Geek from an old post to export data from Access to Excel. I would like to also export the headings and formatting from Access into Excel just like it is possible when doing it manually from inside of Access. has anyone ever done this? I suppose it could be done by writing the code for each field heading, but i wonder if there is an easier way.
I also get an error message "The file you are trying to open is in a different format than specified by the file extension" when i try to open the excel sheet.
How can i open the excel sheet after it has been filled?
Thanks in advance for your help.
Code:
Dim sFileName As String
sFileName = UsersMyDocuments & "\" & Format(Date, "mmmmddyyyy") & ".xls"
Dim oXLApp As Object 'Declare the object variables
Dim oXLBook As Object
Dim oXLSheet As Object
Set oXLApp = CreateObject("Excel.Application") 'Create a new instance of Excel
If Dir(sFileName) = "" Then
Set oXLBook = oXLApp.Workbooks.Add 'File doesnt exist - add a new workbook
Else
Set oXLBook = oXLApp.Workbooks.Open(sFileName) 'File exists - load it
End If
Set oXLSheet = oXLBook.Worksheets(1) 'Work with the first worksheet
oXLSheet.UsedRange.Clear
oXLSheet.Range("A1").CopyFromRecordset RstJobSelected
Set oXLSheet = Nothing 'disconnect from the Worksheet
oXLBook.SaveAs sFileName 'Save (and disconnect from) the Workbook
oXLBook.Close SaveChanges:=False
Set oXLBook = Nothing
oXLApp.Quit 'Close (and disconnect from) Excel
Set oXLApp = Nothing