One common question that I see in the newsgroups is “how can I force Application.GetSaveAsFileName or Application.GetOpenFileName to open in a specific directory ?”. The answer to this is use the ChDrive and ChDir functions.
If you want to open the dialog in say “E:My files”, then this code should do it
Dim Filename As Variant
ChDrive “E”
ChDir “E:My files”
Filename = Application.GetSaveAsFilename()
If TypeName(Filename) <> “Boolean” Then
MsgBox Filename
End If
End Sub
However, this doesn’t work if your path is in a network folder, like
\SomeServerSomefolder
to do this, you need to use the SetCurrentDirectory API function, which is defined as:
Now you can change the current path no matter if the path is mapped to a drive or not. You can also extend this example, saving the current path temporarily, to avoid messing with the user’s settings.
Dim CurrentPath As String
Dim Filename As Variant
‘Store the current path
CurrentPath = CurDir
‘Change the path to the one we want
SetCurrentDirectory “\SomeServerSome Path”
‘Ask for the file name
Filename = Application.GetSaveAsFilename()
If TypeName(Filename) <> “Boolean” Then
MsgBox Filename
End If
‘Change the path back
SetCurrentDirectory CurrentPath
End Sub