1

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.

Community
  • 1
  • 1
GeoffDS
  • 1,221
  • 5
  • 19
  • 31

6 Answers6

6

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.

Kazimierz Jawor
  • 18,861
  • 7
  • 35
  • 55
4

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
1

this could be what you want

dim sStarDir as string
sStarDir=curDir
... do all you stuff
' now reset!
Application.DefaultFilePath=sStarDir

Philip

Our Man in Bananas
  • 5,809
  • 21
  • 91
  • 148
  • When I take away ChDrive and ChDir, and use instead Application.DefaultFilePath, then it just keeps opening in the default MyDocuments and never changes to the directory I want in the first place. Can you provide a bit more detail? – GeoffDS Mar 28 '13 at 18:29
  • what we're doing there is storing the path when the code starts, then after the line *sStarDir=curDir* that is where you **paste** your code including the code for ChDir, and at the end, AFTER your code has finished, we reset the DefaultFilePath with the code *Application.DefaultFilePath=sStarDir* – Our Man in Bananas Apr 02 '13 at 08:42
1

Just put this before the application.getopenfilename():

ChDir "C:"

For example:

ChDir "C:\userjjjj"

myfile = Application.GetOpenFilename()
'Open the file selected
Workbooks.Open (myfile)
Jules Dupont
  • 7,259
  • 7
  • 39
  • 39
0

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)
chris neilsen
  • 52,446
  • 10
  • 84
  • 123
0

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(...)
ascripter
  • 5,665
  • 12
  • 45
  • 68