View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Ken Johnson Ken Johnson is offline
external usenet poster
 
Posts: 1,073
Default Reading sheet names in closed workbooks - Is it possible?

Hi Tom,

I tried this one...

Sub GetSheetNames(rsFilePath As String)
Dim cat As ADOX.Catalog
Dim tbl As ADOX.Table

Set cat = New ADOX.Catalog

cat.ActiveConnection = "Provider=MSDASQL.1;Data Source=" _
& "Excel Files;Initial Catalog=" & rsFilePath
For Each tbl In cat.Tables
If Right$(tbl.Name, 1) = "$" Then _
MsgBox Left$(tbl.Name, Len(tbl.Name) - 1)
Next tbl

Set cat = Nothing
End Sub

Nothing happened.

I discovered that the conditional test...

If Right$(tbl.Name, 1) = "$" Then

failed for every sheet because, for example, with a sheet named John
Smith...

Right$(tbl.Name, 1) = "'", not "$", because tbl.Name = "'John Smith$'",
not "John Smith$".

So, I've had to amend the code to accommodate the leading and trailing
apostrophes...

If Right(tbl.Name, 2) = "$'" Then _
MsgBox Mid(tbl.Name, 2, Len(tbl.Name) - 3)

Any idea as to why I would get these apostrophes that Jake's code
doesn't expect?

Also, Right and Right$ work the same don't they?

Ken Johnson