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

x
?
Solved

Extract Data from Closed XLSX Workbook Using ADO

Posted on 2011-09-30
7
Medium Priority
?
629 Views
Last Modified: 2012-06-21
All,
I wrote this code a long time ago and it works great for 2003 and earlier workbooks.  It extracts data from a closed excel workbook given a named range in the closed workbook.  However, it does not work with 2007 files (xlsx).  I get an error on the cnn.open line.  I understand the file format completely changed from 2003 to 2007.  I have looked an looked and I can't find any examples using ADO with later versions of Excel.

There are three files attached.  The first contains the code below.  The next two are the same except for one is a xls and the other is a xlsx.  Both contain a range named "rngFrom" on Sheet1.  This is the data that gets extracted.

Can someone please help me get this working.  Thanks

Kyle
Sub ExtractData()
Dim cnn As ADODB.Connection
Dim rs1 As ADODB.Recordset
Dim WBCopyFrom As String, rngCopyFrom As String
Dim rngCopyTo As String
Dim PathName As String
Dim strSQL1 As String

'Initiailze variables
'WBCopyFrom = "TestingTesting.xls"   '<--This works
WBCopyFrom = "TestingTesting.xlsx"   '<--This does not work
rngCopyFrom = "rngFrom"
rngCopyTo = "rngTo"

'Create connection to closed workbook
PathName = ThisWorkbook.Path & "\" & WBCopyFrom
Set cnn = New ADODB.Connection
With cnn
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = "Data Source=" & PathName & ";Extended Properties=Excel 8.0;"
    .CursorLocation = adUseClient
    .Open  '<-- Error: "External table is not in the expected format."
End With

'Create SQL string
strSQL1 = "SELECT * FROM [" & rngCopyFrom & "]"

'Create recordset
Set rs1 = New ADODB.Recordset

'Open recordset
rs1.Open strSQL1, cnn, adOpenStatic, adLockOptimistic

'Copy to worksheet
Sheet1.Range(rngCopyTo).CopyFromRecordset rs1

'Clean up
rs1.Close
cnn.Close
End Sub

Open in new window

ExtractFromClosedWB.xlsm
TestingTesting.xls
TestingTesting.xlsx
0
Comment
Question by:kgerb
[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
  • 3
  • 3
7 Comments
 
LVL 19

Expert Comment

by:Arno Koster
ID: 36892061
.XLSX documents are not excel 8.0 but should be excel 12.0 instead.
0
 
LVL 12

Author Comment

by:kgerb
ID: 36892150
Now I get an error "Could not find installable ISAM"???

Kyle
0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 2000 total points
ID: 36892375
If you are also using 2007 or later try this instead
   With mobjConn

        .Provider = "Microsoft.ACE.OLEDB.12.0"

        .ConnectionString = "Data Source=" & wbCopyFrom & ";" & _

                "Extended Properties=Excel 12.0;"

        .Open

    End With

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 17

Expert Comment

by:andrewssd3
ID: 36892388
Got some extra blank lines in there - sorry you'll need to delete them
0
 
LVL 12

Author Comment

by:kgerb
ID: 36892461
andrewssd3,
Thanks, it's working.  Can you explain a little bit.  What's the difference between the Jet provider and the Ace provider?  Why does one work and not the other?

Thanks again,
Kyle
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36892554
The ACE.OLEDB one is the more recent one and works with Office 2007 and 2010 - I'm not sure of the exact details I'm afraid.  It should also be backward compatible
0
 
LVL 12

Author Comment

by:kgerb
ID: 36892567
No problem.  Thanks so much.  I appreciate your help.

Kyle
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

688 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