Solved

Convert XLSX to XLS in VBA

Posted on 2014-02-13
11
1,185 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
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
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 119

Expert Comment

by:Rey Obrero
Comment Utility
try

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

Expert Comment

by:Bill Prew
Comment Utility
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
 

Author Comment

by:correlate
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 119

Accepted Solution

by:
Rey Obrero earned 250 total points
Comment Utility
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
Comment Utility
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 26

Expert Comment

by:MacroShadow
Comment Utility
Same with my suggestion (which is actually the same as Rey Obrero's).
0
 
LVL 26

Assisted Solution

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

Author Comment

by:correlate
Comment Utility
Will give that a go - many thanks
0
 

Author Closing Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now