Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert XLSX to XLS in VBA

Posted on 2014-02-13
11
Medium Priority
?
1,372 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 57

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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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 1000 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 1000 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

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
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 …

670 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