Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1405
  • Last Modified:

Convert XLSX to XLS in VBA

Dear Experts

In order to export an Access query into an .xls spread sheet and not have the fields truncated by the cell limit I have, (courtesy of EE) a piece of code to export the query into an Excel 2010 spread sheet, open in and save it in .xls format ( I need it in this format so the data can be imported into our website).  

This works all fine but with the code below, but I get an error message when I open the newly created .xls spread sheet - (the file you are trying to open is in a different format than specified by the file extension etc.) Any ideas how to solve that.

Private Sub Command2_Click()
DoCmd.OutputTo acOutputQuery, "Tickers for Exporting", acFormatXLSX, "\\nor-gs2-dc01\Redirected\xxxx\Desktop\Tickers for Exporting.xlsx", False
Dim xlObj As Object

Set xlObj = CreateObject("Excel.Application")
    xlObj.Workbooks.Open "\\nor-gs2-dc01\Redirected\xxxx\Desktop\Tickers for Exporting.xlsx"
    With xlObj
        .activeworkbook.SaveAs "\\nor-gs2-dc01\Redirected\xxxx\Desktop\Tickers for Exporting.xls"
    End With
    xlObj.Quit
    Set xlObj = Nothing
End Sub

Open in new window

0
correlate
Asked:
correlate
  • 5
  • 3
  • 2
  • +1
2 Solutions
 
MacroShadowCommented:
Replace line 8 with this:
.activeworkbook.SaveAs Filename:="\\nor-gs2-dc01\Redirected\xxxx\Desktop\Tickers for Exporting.xls", FileFormat:=xlExcel8

Open in new window

0
 
Rey Obrero (Capricorn1)Commented:
try

    With xlObj
        .activeworkbook.SaveAs "\\nor-gs2-dc01\Redirected\xxxx\Desktop\Tickers for Exporting.xls", fileformat:=56
    End With
0
 
Bill PrewCommented:
Well, if you are exceeding limits of the older XLS format in terms of number of rows, or number of cells, I',m not sure that the approach you are taking will work.  You worked around the limit in Access by saving as XLSX, but I suspect when you try to convert back to XLS with Excel you will have a problem at the limits.

Have you tried opening the XLSX in Excel on the screen, and doing a "Save As" to the older version XLS format?  Does it display any errors or warnings, and is the saved file correct?

~bp
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
correlateAuthor Commented:
Hi All,

Apologies for the delay - the 2 earlier methods seem to freeze Access & Excel - got to head out for an hour but will give it a further go once I'm back.

Hi BP - have tried that & it works all fine, so in theory it should all be fine
0
 
correlateAuthor Commented:
Hi

I've run some more tests, the code only works if there is already an existing .xls spreadsheet with the same name, otherwise if just freezes - Maybe its got something to do with the compatibility checker?

Any ideas
0
 
Rey Obrero (Capricorn1)Commented:
not sure why you are getting the problem, this codes work fine here


    With xlObj
        .activeworkbook.SaveAs "\\nor-gs2-dc01\Redirected\xxxx\Desktop\Tickers for Exporting.xls", fileformat:=56
    End With


can you try saving the excel file in your local drive..
0
 
correlateAuthor Commented:
Hi thanks

Did that as part of the testing, but it made no difference.  

I think what I might do is export the query to xls, then export it to xlsx export and then open it & do the save as piece - that way there is always a copy to overwrite - the only downside is I get excel "do you want to overwrite this file" messages, but can live with that if needs be
0
 
MacroShadowCommented:
Same with my suggestion (which is actually the same as Rey Obrero's).
0
 
MacroShadowCommented:
You can disable the warning by using Application.AlertBeforeOverwriting = False
0
 
correlateAuthor Commented:
Will give that a go - many thanks
0
 
correlateAuthor Commented:
Thanks for all your help - works a treat now & apologies for the delay in closing this one out
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now