DoCmd.OutputTo in xlsx (Excel 2007+) Format

https://www.devhut.net/2014/08/07/docmd-outputto-in-xlsx-excel-2007-format/


A common technique for exporting data to Excel is to utilize the  DoCmd.OutputTo Method.
Now, previously (pre Office 2007), the basic format to export a query to Excel format was simply:
DoCmd.OutputTo acOutputQuery, "YourQueryName", acFormatXLS, , True
BUT, once you port such a database to a post Office 2007 computer, you start to get it exported as a “Microsoft Excel 5.0/95 Workbook (*.xls)” format and such a file will thus open Excel in compatibility mode possibly given the user compatibility errors/messages when they go to save the file after working with it.
So unless you have to ensure legacy compatibility, it is strongly advisable to update your code to export the data in “Excel Workbook (*.xlsx)” format, the new Excel file format thus eliminating any compatibility issues. Hence, we simply need to change the OutputFormat format variable to a more appropriate format (acFormatXLS -> acFormatXLSX). So your code would thus become:
DoCmd.OutputTo acOutputQuery, "YourQueryName", acFormatXLSX, , True
Alternately, you could also use:
DoCmd.OutputTo acOutputQuery, "YourQueryName", "Excel Workbook (*.xlsx)", , True

Looking to export to a different format?

Nothing could be easier! In the VBA Object browser, go to the Access Contants library and look over the various acFormat**** available to you. Such as:
  • acFormatHTML
  • acFormatPDF
  • acFormatRTF
  • acFormatSNP
  • acFormatTXT
  • acFormatXLS
  • acFormatXLSB
  • acFormatXLSX
  • acFormatPS

Taking Things a Little Further

It wouldn’t be very difficult to create a custom DoCmd.OutputTo function which could determine the version of MS Excel installed and the utilize the more appropriate OutputFormat variable. Then you code would be 100% compatible regardless of what your users have installed!

Are There Any Alternatives

Another approach available to us is simply automate Excel. This is the approach I have adopted. In my early development days, I used the built-in DoCmd.OutputTo command, but have long since abandoned it for my own custom export function: Export2XLS. Why? The custom function approach is fully compatible with whichever version of Excel a user has installed, and gives the programmer far more control over what the final export looks like. You can format Cells/Ranges, apply colors, fonts, …

Comments

Popular posts from this blog

MS Access: export a query to Excel with Docmd.Output

Export Access Object to Excel ,PDF, RTF , etc. using DoCmd.OutputTo