Path and Name

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello to all!

Here is my problem: I need to know the name and path of a file that is
selected through "Application.GetOpenFilename()." However whenever I try to
run the code "Run-time error '9': Subscript out of range" message comes up.
I thought that it could be the file path is too long(files on the network)
however when I selected a local file and my hd the same message still came up.

Here is the code:
Private Sub CommandButton1_Click()
Dim file As String, Path1 As String, name As String
file = Application.GetOpenFilename()
Path1 = Workbooks(file).Path
name = Workbooks(file).Name
End Sub
 
That is because the Workbooks collection pertains to OPEN workbooks. Your
code hasn't opened the filename retrieved by GetOpenFilename. Either open it
and use your c ode, or parse it manually.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
Continuing on from Bob's reply:
If you want the file to open then use:

Private Sub CommandButton2_Click()
Dim file As String, Path1 As String, name As String
file = Application.GetOpenFilename()
Workbooks.Open file
Path1 = ActiveWorkbook.Path
name = ActiveWorkbook.name
End Sub

If you don't wnt the file open first, use the split function:

ie file2=split(file,"\")

and then look at all the elements of array file2.

ChasAA
 
Thanks for the help guys!

ChasAA said:
Continuing on from Bob's reply:
If you want the file to open then use:

Private Sub CommandButton2_Click()
Dim file As String, Path1 As String, name As String
file = Application.GetOpenFilename()
Workbooks.Open file
Path1 = ActiveWorkbook.Path
name = ActiveWorkbook.name
End Sub

If you don't wnt the file open first, use the split function:

ie file2=split(file,"\")

and then look at all the elements of array file2.

ChasAA
 
Back
Top