Setting default directory prior to GetOpenFilename

D

David R

When using GetOpenFilename the panel displays files in a
particular directory, but I want it to open displaying the
files in a sub-directory of that one. I have tried using
ChDir to set the default, but it doesn't work (probably
because my directory paths are all in UNC form). How can
I change the default directory prior to calling
GetOpenFilename?
 
H

Hank Scorpio

When using GetOpenFilename the panel displays files in a
particular directory, but I want it to open displaying the
files in a sub-directory of that one. I have tried using
ChDir to set the default, but it doesn't work (probably
because my directory paths are all in UNC form). How can
I change the default directory prior to calling
GetOpenFilename?

I can't say that I've had that kind of problem with ChDir myself. Have
you tried this syntax:

chdir curdir & "/SubfolderName"

That may help to resolve the problem, since it'll use whatever the
current PC sees as the current path, regardless of the UNC path.

Usually the only "Gotcha" with ChDir is that it won't work if the
directory that you want to set as the current one is on a different
DRIVE to the current one. In that case you need to use a ChDrive
command first. However if the parent folder is opening when you use
the GetOpenFileName method, that's unlikely to be the problem here.
 
T

Tom Ogilvy

Your right, chdir doesn't work with a UNC path. Here is some code
previously posted by Rob Bovey:

Private Declare Function SetCurrentDirectoryA Lib _
"kernel32" (ByVal lpPathName As String) As Long

Sub ChDirNet(szPath As String)
Dim lReturn As Long
lReturn = SetCurrentDirectoryA(szPath)
Debug.Print lReturn
If lReturn = 0 Then Err.Raise vbObjectError + 1, "Error setting path."
End Sub

' usage example
Sub FindFile()
ChDirNet "\\LOGD0FILES\OGILVTW\Docs\Temp"
fName = Application.GetOpenFileName()
if fName <> False then
msgbox fName
End if
End Sub

the declaration must go at the top of a general module.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top