Excel VBA 更改 Application.GetOpenFilename 的默认目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15684950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA change default directory for Application.GetOpenFilename
提问by GeoffDS
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
我用谷歌搜索并找到了我的部分问题的答案,但没有找到完整的问题。我想在 Excel VBA 中使用 Application.GetOpenFilename 打开一个文件,我希望它在与 ThisWorkbook.Path 相同的目录中打开。我发现事先我可以做到
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.
但是,在运行之后,如果我运行任何其他 Application.GetOpenFilename,它仍然会访问同一个目录(直到我关闭 Excel ???)。但是,我希望它恢复到默认目录(无论是什么)。在我的 Windows XP 计算机上,它恰好是 MyDocuments。但是,使用它的一些人可能有 XP,有些人可能有 Windows 7。我找不到任何地方如何弄清楚原始默认目录是什么,以便我可以存储它,以便我以后可以重置回默认值. 任何帮助将非常感激。
回答by Kazimierz Jawor
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
.
如有必要,请与Drive
.
回答by Buntes Lama
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:
我在回答这个问题时遇到了一些困难,因为 ChDir 和 ChDrive 在我的网络文件夹中不起作用。这是我在论坛上发现的,它运行得非常好:
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
回答by Jo?o álvares Ribeiro
Just put this before the application.getopenfilename()
:
只需将其放在以下内容之前application.getopenfilename()
:
ChDir "C:"
For example:
例如:
ChDir "C:\userjjjj"
myfile = Application.GetOpenFilename()
'Open the file selected
Workbooks.Open (myfile)
回答by Our Man in Bananas
this could be what you want
这可能是你想要的
dim sStarDir as string
sStarDir=curDir
... do all you stuff
' now reset!
Application.DefaultFilePath=sStarDir
Philip
菲利普
回答by Jorge O. Gómez
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)