Link to home
Start Free TrialLog in
Avatar of kgerb
kgerbFlag for United States of America

asked on

Extract Data from Closed XLSX Workbook Using ADO

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
Avatar of Arno Koster
Arno Koster
Flag of Netherlands image

.XLSX documents are not excel 8.0 but should be excel 12.0 instead.
Avatar of kgerb

ASKER

Now I get an error "Could not find installable ISAM"???

Kyle
ASKER CERTIFIED SOLUTION
Avatar of andrewssd3
andrewssd3
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Got some extra blank lines in there - sorry you'll need to delete them
Avatar of kgerb

ASKER

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
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
Avatar of kgerb

ASKER

No problem.  Thanks so much.  I appreciate your help.

Kyle