keepITcool said:
opening could trigger recalcs and recompiles maybe adox wouldnt be
that bad..
Agreed.
Following is a starter for the ADOX way
collects all books with sheets from current directory not fully
tested..
Noted. You have fallen foul of the usual gotchas <g> :
1) The $ character, in common with the single quote ' character, is
legal in a worksheet name e.g. in my Excel test database workbook I
have the following worksheet names as 'seen' by Jet:
'Sheet Name Has $ dollar and gap$'
' ''$$'
'$$'
2) Worksheet-level defined Names appear as Excel tables and the $ is
used as a delimiter between sheet name and Name name ($ is illegal in
Name names) e.g. in my database I have the following sheet-level
names:
EarningsHistory$Table1
'Sheet Name Has $ dollar and gap$'MyName
' ''$$'MyName
So your code returns items that aren't worksheets and changes the
names of some worksheets <g>.
Here's my attempt (again, not fully tested):
Public Function GetWSNames( _
ByVal WBPath As String _
) As Variant
Dim adCn As Object
Dim adRs As Object
Dim asSheets() As String
Dim nShtNum As Long
Dim nRows As Long
Dim nRowCounter As Long
Dim sSheet As String
Dim sOSheet As String
Dim sChar1 As String
Dim sChar2 As String
Const INDICATOR_SHEET As String = "$"
Const INDICATOR_SPACES As String = "'"
Set adCn = CreateObject("ADODB.Connection")
With adCn
.ConnectionString = "Provider=Microsoft.Jet.OLEDB" & _
".4.0;Data Source=" & WBPath & ";Extended " & _
"Properties='Excel 8.0;HDR=Yes'"
.CursorLocation = 3
.Open
End With
Set adRs = adCn.OpenSchema(20)
With adRs
nRows = .RecordCount
Dim strMsg As String
For nRowCounter = 0 To nRows - 1
sOSheet = !TABLE_NAME
strMsg = "[" & sOSheet & "]"
sSheet = !TABLE_NAME
sChar1 = vbNullString
sChar2 = vbNullString
On Error Resume Next
sChar1 = Mid$(sSheet, Len(sSheet), 1)
sChar2 = Mid$(sSheet, Len(sSheet) - 1, 1)
On Error GoTo 0
Select Case sChar1
Case INDICATOR_SHEET
sSheet = Left$(sSheet, Len(sSheet) - 1)
Case INDICATOR_SPACES
If sChar2 = INDICATOR_SHEET Then
sSheet = Mid$(sSheet, 2, Len(sSheet) - 3)
End If
Case Else
sSheet = vbNullString
End Select
If Len(sSheet) > 0 Then
ReDim Preserve asSheets(nShtNum)
' Un-escape
asSheets(nShtNum) = Replace(sSheet, _
INDICATOR_SPACES & INDICATOR_SPACES, _
INDICATOR_SPACES)
strMsg = strMsg & "=[" & sSheet & "]"
nShtNum = nShtNum + 1
End If
.MoveNext
Next
.Close
End With
adCn.Close
GetWSNames = asSheets
End Function
Jamie.
--