Solved

Convert XLSX to XLS in VBA

Posted on 2014-02-13
11
1,221 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 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 53

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

770 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