【问题标题】:How to get current working directory using vba?如何使用 vba 获取当前工作目录?
【发布时间】:2013-11-18 09:40:44
【问题描述】:

我正在使用 MS Excel 2010 并尝试使用以下代码获取当前目录,

    path = ActiveWorkbook.Path

但 ActiveWorkbook.Path 返回空白。

【问题讨论】:

  • 如果文件尚未保存,则没有返回路径。 “当前”目录是什么意思?如果你只想要当前的 default 目录,你可以使用CurDir()

标签: excel vba


【解决方案1】:

ActiveWorkbook 似乎没有保存...

改用CurDir()

【讨论】:

  • 这通常是正确的,除非已使用明确设置的替代工作目录打开 Excel。如果活动工作簿尚未保存,则完全由您决定保存的位置。
【解决方案2】:

您有多种选择,具体取决于您要查找的内容。 Workbook.Path 返回已保存工作簿的路径。 Application.Path 返回 Excel 可执行文件的路径。 CurDir 返回当前工作路径,这可能默认为您的“我的文档”文件夹或类似文件夹。

您还可以使用 Windows 脚本外壳对象的 .CurrentDirectory 属性。

Set wshell = CreateObject("WScript.Shell")
Debug.Print wshell.CurrentDirectory

但这应该得到和刚才一样的结果

Debug.Print CurDir

【讨论】:

    【解决方案3】:

    我已经测试过了:

    当我打开一个 Excel 文档D:\db\tmp\test1.xlsm:

    • CurDir() 返回C:\Users\[username]\Documents

    • ActiveWorkbook.Path 返回D:\db\tmp

    所以CurDir() 有一个系统默认值,可以更改。

    ActiveWorkbook.Path 对于保存的同一个工作簿不会更改。

    例如,CurDir() 在您执行“文件/另存为”命令时发生变化,并在“文件/目录”选择对话框中选择一个随机目录。然后单击取消跳过保存。但是CurDir()已经变成了上次选择的目录。

    【讨论】:

    • 当你使用Workbooks.Open("file.xls")(不给出路径)时,是否假定CurDir()为路径?
    • 对于使用 MS Access 的开发人员:Application.CurrentProject.Path
    【解决方案4】:

    使用这些代码并享受它。

    Public Function GetDirectoryName(ByVal source As String) As String()
    Dim fso, oFolder, oSubfolder, oFile, queue As Collection
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set queue = New Collection
    
    Dim source_file() As String
    Dim i As Integer        
    
    queue.Add fso.GetFolder(source) 'obviously replace
    
    Do While queue.Count > 0
        Set oFolder = queue(1)
        queue.Remove 1 'dequeue
        '...insert any folder processing code here...
        For Each oSubfolder In oFolder.SubFolders
            queue.Add oSubfolder 'enqueue
        Next oSubfolder
        For Each oFile In oFolder.Files
            '...insert any file processing code here...
            'Debug.Print oFile
            i = i + 1
            ReDim Preserve source_file(i)
            source_file(i) = oFile
        Next oFile
    Loop
    GetDirectoryName = source_file
    End Function
    

    在这里你可以调用函数:

    Sub test()
    Dim s
    For Each s In GetDirectoryName("C:\New folder")
    Debug.Print s
    Next
    End Sub
    

    【讨论】:

      【解决方案5】:

      Application.ActiveWorkbook.Path 仅用于路径本身(不带工作簿名称)或Application.ActiveWorkbook.FullName 用于带有工作簿名称的路径。

      【讨论】:

        【解决方案6】:

        您的代码:path = ActiveWorkbook.Path

        返回空白,因为您尚未保存工作簿。

        要解决您的问题,请返回 Excel 工作表,保存工作表,然后再次运行代码。

        这次它不会显示空白,而是会显示它所在的路径(当前文件夹)

        希望对你有所帮助。

        【讨论】:

          【解决方案7】:

          这是我用来在 Explorer 窗口中打开当前路径的 VBA:

          Shell Environ("windir") & "\explorer.exe """ & CurDir() & "",vbNormalFocus
          

          微软文档

          【讨论】:

            【解决方案8】:

            如果你真的是指纯粹的工作目录,这应该适合你。

            解决方案 A:

            Dim ParentPath As String: ParentPath = "\"
            Dim ThisWorkbookPath As String
            Dim ThisWorkbookPathParts, Part As Variant
            Dim Count, Parts As Long
            
            ThisWorkbookPath = ThisWorkbook.Path
            ThisWorkbookPathParts = Split(ThisWorkbookPath, _
                                    Application.PathSeparator)
            
            Parts = UBound(ThisWorkbookPathParts)
            Count = 0
            For Each Part In ThisWorkbookPathParts
                If Count > 0 Then
                    ParentPath = ParentPath & Part & "\"
                End If
                Count = Count + 1
                If Count = Parts Then Exit For
            Next
            
            MsgBox "File-Drive = " & ThisWorkbookPathParts _
                   (LBound(ThisWorkbookPathParts))
            MsgBox "Parent-Path = " & ParentPath
            

            但如果不这样做,这应该足够了。

            解决方案 B:

            Dim ThisWorkbookPath As String
            
            ThisWorkbookPath = ThisWorkbook.Path
            MsgBox "Working-Directory = " & ThisWorkbookPath 
            

            【讨论】:

              【解决方案9】:

              下面的简单示例:

              Sub openPath()
              Dim path As String
              path = Application.ActivePresentation.path
              Shell Environ("windir") & "\explorer.exe """ & path & "", vbNormalFocus
              End Sub
              

              【讨论】:

              • 问题的目标是了解当前路径是什么以编程方式并将其放入变量中。您的解决方案只是打开 Windows 资源管理器以向用户显示什么路径是无用的,因为它不能随后以编程方式使用。 ActivePresentation 也不是 excel 应用程序的属性,因此甚至无法编译/运行
              猜你喜欢
              • 2013-06-25
              • 2016-04-05
              • 2013-05-14
              • 1970-01-01
              • 2011-06-19
              • 2014-03-10
              • 2021-01-18
              相关资源
              最近更新 更多