'Excel VBA change default directory for Application.GetOpenFilename
I have googled and found answers to part of my question but not the complete question. I want to use Application.GetOpenFilename in Excel VBA to open a file and I want it to open in the same directory as ThisWorkbook.Path. I have found that beforehand I can do
OpenPath = ThisWorkbook.Path
ChDrive OpenPath
ChDir OpenPath
But, after that runs, if I run any other Application.GetOpenFilename it will still access that same directory (until perhaps I close Excel???). But, I want it to revert back to the default directory (no matter what that was). On my computer, which is Windows XP, it happens to be MyDocuments. But, some of the people using this may have XP and some may have Windows 7. I can't find anywhere how to figure out what the original default directory was so that I can store this so that I can later reset back to the default. Any help would be much appreciated.
Solution 1:[1]
So, this could be solution:
Dim StartingDir as string
StartingDir = CurDir
'...your code here
ChDir StartingDir 'just before you leave
And if necessary do similar with Drive.
Solution 2:[2]
I had some struggle answering this question, because ChDir and ChDrive did not work in my network folder. This is what I found in a forum, it's working surprisingly well:
Private Declare Function SetCurrentDirectoryA Lib _
"kernel32" (ByVal lpPathName As String) As Long
Sub SetUNCPath(sPath As String)
Dim lReturn As Long
lReturn = SetCurrentDirectoryA(sPath)
If lReturn = 0 Then MsgBox "Error setting path."
End Sub
Solution 3:[3]
this could be what you want
dim sStarDir as string
sStarDir=curDir
... do all you stuff
' now reset!
Application.DefaultFilePath=sStarDir
Philip
Solution 4:[4]
Just put this before the application.getopenfilename():
ChDir "C:"
For example:
ChDir "C:\userjjjj"
myfile = Application.GetOpenFilename()
'Open the file selected
Workbooks.Open (myfile)
Solution 5:[5]
Use the next code is working
' declare the variable as string
Dim ruta As String
' get the dir of the current workbook
ruta = ThisWorkbook.Path & "\"
' this line set the dir as the same of the workbook
ChDir ruta
' open a book in the same directory of the current book
Workbooks.Open(Application.GetOpenFilename)
Solution 6:[6]
For network paths ChDir doesn't work. The solution of @Buntes-Lama works
Private Declare Function SetCurrentDirectoryA Lib "kernel32" (ByVal lpPathName As String) As Long
on VBA 7 systems you need to add the PtrSafe keyword to the Declare statement
Private Declare PtrSafe Function SetCurrentDirectoryA Lib "kernel32" (ByVal lpPathName As String) As Long
Explanation: You essentially make the function SetCurrentDirectoryA from the external library kernel32 accessible in your module.
Complete answer to the question (also checking VBA-version via conditional compilation):
#If VBA7 Then
Private Declare PtrSafe Function SetCurrentDirectoryA Lib "kernel32" (ByVal lpPathName As String) As Long
#Else
Private Declare Function SetCurrentDirectoryA Lib "kernel32" (ByVal lpPathName As String) As Long
#End If
SetCurrentDirectoryA (ThisWorkbook.Path) ' Path can be a network directory
Application.GetOpenFilename(...)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kazimierz Jawor |
| Solution 2 | Buntes Lama |
| Solution 3 | Our Man in Bananas |
| Solution 4 | Jules Dupont |
| Solution 5 | chris neilsen |
| Solution 6 | ascripter |
