Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of CurDir vs FSO to retrieve a file path

Status
Not open for further replies.

phudgens

Technical User
Jul 8, 2004
117
US
I am using the following code to retrieve the full path of a user specified file:

Code:
With Application
    FileName = .GetOpenFilename(Filter, FilterIndex, Title)
    FilesFolder = CurDir()
    FilesDrive = Trim(Mid(FilesFolder, 1, 1))
    ChDrive (Left(.DefaultFilePath, 1))
    ChDir (.DefaultFilePath)
End With

This works in Excel 2003 VBA, but knowing that CurDir is older technology, and assuming that it will not work with Excel 2007 VBA, I have been trying to implement the FSO technology via the following code:

Code:
Set fs1 = CreateObject("Scripting.FileSystemObject")
Set FPath = fs.GetFile(FileName)
FilesFolder = FPath.Path
FilesDrive = Trim(Mid(FilesFolder, 1, 1))

This code does not work because FileName must be dimmed as an Oject. But if it is dimmed as an object, it no longer works with the GetOpenFileName command. Ultimately, I am just trying to retrieve the full path of the user selected file (preferably as a string) so that it can be used with other GetOpenFileName commands. Does anyone know the solution here?
Thanks,
Paul Hudgens
Denver
 
This code does not work because FileName must be dimmed as an Oject. "

What makes you think that? File in the context as a parameter of GetFile is a string.
Code:
Dim fso As Scripting.FileSystemObject
' yes THIS is an object
Dim fil As Scripting.File
Dim FileName As String

FileName = "c:\zzz\whatever.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fil = fso.GetFile(FileName)

' it could also be done directly - as a string - as
Set fil = fso.GetFile("c:\zzz\whatever.txt")

Gerry
 
I set this code up as you specify and then tried fil.Path to get the path of the file, but that gives me the path of the file plus the name of the file. I can get the path alone from that if needed, but I'm wondering if there is a different option to use that will give me the path by itself. I've searched, but have been unable to find a thorough listing of all the options that can be used in conjuntion with FileSystemOject - do you perhaps know where I can find one?

THanks,
Paul H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top