Little faux-pas in there, it used an Access connection string.
Here is a corrected version that access Excel spreadsheets. Otherwise it is
the same.
I have also included an ADO version just to show the difference.
'---------------------------------------------------------------------------
---------
' ADOX Version
'---------------------------------------------------------------------------
---------
Function NumSheetsADOX(FileName As String) As Long
Dim oConn As Object
Dim oCat As Object
Dim oTable As Object
Dim sConnString As String
Dim sFileName As String
sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & FileName & ";" & _
"Extended Properties=Excel 8.0;"
Set oConn = CreateObject("ADODB.Connection")
On Error Resume Next
oConn.Open sConnString
If Err.Number <> 0 Then
NumSheetsADOX = -1
Exit Function
Else
On Error GoTo 0
Set oCat = CreateObject("ADOX.Catalog")
Set oCat.ActiveConnection = oConn
NumSheetsADOX = oCat.tables.Count
End If
oConn.Close
Set oCat = Nothing
Set oConn = Nothing
End Function
'---------------------------------------------------------------------------
---------
' ADO Version
'---------------------------------------------------------------------------
---------
Function NumSheetsADO(FileName As String) As Long
Dim oConn As Object
Dim oRS As Object
Dim sConnString As String
Dim sFileName As String
Dim cTables As Long
sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & FileName & ";" & _
"Extended Properties=Excel 8.0;"
Set oConn = CreateObject("ADODB.Connection")
On Error Resume Next
oConn.Open sConnString
If Err.Number <> 0 Then
NumSheetsADO = -1
Exit Function
Else
On Error GoTo 0
Set oRS = oConn.OpenSchema(20, _
Array(Empty, Empty, Empty, "Table"))
Do While Not oRS.EOF
cTables = cTables + 1
oRS.MoveNext
Loop
End If
NumSheetsADO = cTables
oConn.Close
Set oRS = Nothing
Set oConn = Nothing
End Function
--
HTH
Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)