Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21100

Help export data from Access with formatting and layout to Excel

$
0
0
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.

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


Viewing all articles
Browse latest Browse all 21100

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>