【问题标题】:Excel VBA change default directory for Application.GetOpenFilenameExcel VBA 更改 Application.GetOpenFilename 的默认目录
【发布时间】:2013-03-28 14:50:16
【问题描述】:

我已经用谷歌搜索并找到了部分问题的答案,但没有找到完整问题的答案。我想在 Excel VBA 中使用 Application.GetOpenFilename 打开一个文件,我希望它在与 ThisWorkbook.Path 相同的目录中打开。我事先发现我可以做到

OpenPath = ThisWorkbook.Path
ChDrive OpenPath
ChDir OpenPath

但是,在运行之后,如果我运行任何其他 Application.GetOpenFilename 它仍然会访问同一个目录(直到我关闭 Excel ???)。但是,我希望它恢复到默认目录(不管那是什么)。在我的计算机上,它是 Windows XP,它恰好是 MyDocuments。但是,使用它的一些人可能有 XP,而另一些人可能有 Windows 7。我找不到任何地方如何找出原始默认目录是什么,以便我可以存储它,以便以后可以重置为默认值.任何帮助将不胜感激。

【问题讨论】:

  • @KazJaw 提出了一个好方法。或者你也可以使用SetCurrentDirectory API :)
  • 他不能使用Application.DefaultFilePath吗?
  • 给猫剥皮的多种方法 :)
  • 只要猫不动!

标签: excel excel-2007 vba


【解决方案1】:

所以,这可能是解决方案:

Dim StartingDir as string
    StartingDir = CurDir

'...your code here

ChDir StartingDir    'just before you leave

如有必要,请使用 Drive 进行类似操作。

【讨论】:

  • + 1 这是最简单的方法:)
【解决方案2】:

我很难回答这个问题,因为 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

【讨论】:

    【解决方案3】:

    这可能是你想要的

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

    菲利普

    【讨论】:

    • 当我拿走 ChDrive 和 ChDir 并改用 Application.DefaultFilePath 时,它只会在默认的 MyDocuments 中保持打开状态,并且从不首先更改到我想要的目录。你能提供更多细节吗?
    • 我们在这里所做的是在代码开始时存储路径,然后在 sStarDir=curDir 行之后粘贴你的包含 ChDir 代码的代码,最后,在您的代码完成后,我们使用代码 Application.DefaultFilePath=sStarDir 重置 DefaultFilePath
    【解决方案4】:

    把这个放在application.getopenfilename()之前:

    ChDir "C:"
    

    例如:

    ChDir "C:\userjjjj"
    
    myfile = Application.GetOpenFilename()
    'Open the file selected
    Workbooks.Open (myfile)
    

    【讨论】:

      【解决方案5】:

      使用下一个代码是有效的

      ' 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)
      

      【讨论】:

        【解决方案6】:

        对于网络路径ChDir 不起作用。 @Buntes-Lama 的解决方案有效

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

        在 VBA 7 系统上,您需要将 PtrSafe 关键字添加到 Declare 语句

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

        说明:您实际上使来自外部库kernel32 的函数SetCurrentDirectoryA 在您的模块中可访问。

        问题的完整答案(也通过conditional compilation检查VBA版本):

        #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(...)
        

        【讨论】:

          猜你喜欢
          • 2014-05-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-07
          • 1970-01-01
          • 2015-01-05
          相关资源
          最近更新 更多