Solved

Convert XLSX to XLS in VBA

Posted on 2014-02-13
11
1,258 Views
Last Modified: 2014-03-07
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
Comment
Question by:correlate
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39855990
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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39856004
try

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

Expert Comment

by:Bill Prew
ID: 39856045
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
Independent Software Vendors: 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!

 

Author Comment

by:correlate
ID: 39856065
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
 

Author Comment

by:correlate
ID: 39856456
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
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 250 total points
ID: 39856609
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
 

Author Comment

by:correlate
ID: 39856635
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
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39856638
Same with my suggestion (which is actually the same as Rey Obrero's).
0
 
LVL 27

Assisted Solution

by:MacroShadow
MacroShadow earned 250 total points
ID: 39856648
You can disable the warning by using Application.AlertBeforeOverwriting = False
0
 

Author Comment

by:correlate
ID: 39856658
Will give that a go - many thanks
0
 

Author Closing Comment

by:correlate
ID: 39912154
Thanks for all your help - works a treat now & apologies for the delay in closing this one out
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question