Excel connection strings
https://www.connectionstrings.com/excel/
Při použití metody Connection.OpenSchema postačí Connection a Recordset, instanci connection vytvoří CreateObject, tak není nutné přidat reference do VBA projektu.
Function GetWorksheetNames(ByVal file_path As String) As Collection
Dim cn As Object, rs As Object
Set GetWorksheetNames = New Collection
Set cn = CreateObject("ADODB.Connection")
a_connection_string = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};" & _
"Extended Properties=""Excel 12.0 {1};HDR=YES"";"
a_connection_string = Replace(a_connection_string, "{0}", file_path)
If Right(file_path, 4) = "xlsm" Then
a_connection_string = Replace(a_connection_string, "{1}", "Macro")
Else
a_connection_string = Replace(a_connection_string, "{1}", "Xml")
End If
cn.Open a_connection_string
Set rs = cn.OpenSchema(20) 'adSchemaTables=20
Do While Not rs.EOF
GetWorksheetNames.Add Left(rs("TABLE_NAME"), Len(rs("TABLE_NAME")) - 1) 'without $
rs.MoveNext
Loop
If cn.State = 1 Then 'adStateOpen=1
cn.Close
End If
Set rs = Nothing
Set cn = Nothing
End Function
Sub CallFunctionAndReturnNames()
Dim worksheet_names As Collection, i As Integer
Set worksheet_names = GetWorksheetNames("C:\Sešit.xlsx")
ReDim array_names(1 To worksheet_names.Count)
For i = 1 To worksheet_names.Count
array_names(i) = worksheet_names(i)
Next
MsgBox Join(array_names, ",")
End Sub
Těžko říct, snad bude trochu rychlejší, třeba to zkuste.
citovat