How to get current working directory using vba?












48















I am using MS Excel 2010 and trying to get the current directory using the below code,



    path = ActiveWorkbook.Path


But ActiveWorkbook.Path returns blank.










share|improve this question




















  • 2





    If the file hasn't yet been saved then there's no path to return. What do you mean by "current" directory? If you just want the current default directory you can use CurDir()

    – Tim Williams
    Nov 6 '13 at 22:29


















48















I am using MS Excel 2010 and trying to get the current directory using the below code,



    path = ActiveWorkbook.Path


But ActiveWorkbook.Path returns blank.










share|improve this question




















  • 2





    If the file hasn't yet been saved then there's no path to return. What do you mean by "current" directory? If you just want the current default directory you can use CurDir()

    – Tim Williams
    Nov 6 '13 at 22:29
















48












48








48


5






I am using MS Excel 2010 and trying to get the current directory using the below code,



    path = ActiveWorkbook.Path


But ActiveWorkbook.Path returns blank.










share|improve this question
















I am using MS Excel 2010 and trying to get the current directory using the below code,



    path = ActiveWorkbook.Path


But ActiveWorkbook.Path returns blank.







excel vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 '13 at 8:00









Jean-François Corbett

28.7k22110160




28.7k22110160










asked Nov 6 '13 at 22:26









UllanUllan

3282717




3282717








  • 2





    If the file hasn't yet been saved then there's no path to return. What do you mean by "current" directory? If you just want the current default directory you can use CurDir()

    – Tim Williams
    Nov 6 '13 at 22:29
















  • 2





    If the file hasn't yet been saved then there's no path to return. What do you mean by "current" directory? If you just want the current default directory you can use CurDir()

    – Tim Williams
    Nov 6 '13 at 22:29










2




2





If the file hasn't yet been saved then there's no path to return. What do you mean by "current" directory? If you just want the current default directory you can use CurDir()

– Tim Williams
Nov 6 '13 at 22:29







If the file hasn't yet been saved then there's no path to return. What do you mean by "current" directory? If you just want the current default directory you can use CurDir()

– Tim Williams
Nov 6 '13 at 22:29














8 Answers
8






active

oldest

votes


















81














I've tested this:



When I open an Excel document D:dbtmptest1.xlsm:




  • CurDir() returns C:Users[username]Documents


  • ActiveWorkbook.Path returns D:dbtmp



So CurDir() has a system default and can be changed.



ActiveWorkbook.Path does not change for the same saved Workbook.



For example, CurDir() changes when you do "File/Save As" command, and select a random directory in the File/Directory selection dialog. Then click on Cancel to skip saving. But CurDir() has already changed to the last selected directory.






share|improve this answer


























  • When you use Workbooks.Open("file.xls") (without giving a path), does it assume CurDir() as the path?

    – André Chalella
    Jul 9 '15 at 9:38






  • 3





    For developers using MS Access: Application.CurrentProject.Path

    – duckboy81
    Feb 13 '17 at 4:06





















8














You have several options depending on what you're looking for.
Workbook.Path returns the path of a saved workbook. Application.Path returns the path to the Excel executable. CurDir returns the current working path, this probably defaults to your My Documents folder or similar.



You can also use the windows scripting shell object's .CurrentDirectory property.



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


But that should get the same result as just



Debug.Print CurDir





share|improve this answer































    7














    It would seem likely that the ActiveWorkbook has not been saved...



    Try CurDir() instead.






    share|improve this answer
























    • CurDir() returns user's profile directory

      – Ullan
      Nov 6 '13 at 22:46











    • That is usually correct, unless Excel has been opened with an explicitly set alternative working directory. If the active workbook has not been saved, then it is entirely up to you as to where it is to be saved.

      – Monty Wild
      Nov 6 '13 at 22:48





















    4














    Your code: path = ActiveWorkbook.Path



    returns blank because you haven't saved your workbook yet.



    To overcome your problem, go back to the Excel sheet, save your sheet, and run your code again.



    This time it will not show blank, but will show you the path where it is located (current folder)



    I hope that helped.






    share|improve this answer































      1














      Use Application.ActiveWorkbook.Path for just the path itself (without the workbook name) or Application.ActiveWorkbook.FullName for the path with the workbook name.






      share|improve this answer































        0














        This is the VBA that I use to open the current path in an Explorer window:



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




        Microsoft Documentation:





        • CurDir Function


        • Environ Function

        • Shell Function






        share|improve this answer































          0














          If you really mean pure working Directory, this should suit for you.



          Solution 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


          But if don't, this should be enough.



          Solution B:



          Dim ThisWorkbookPath As String

          ThisWorkbookPath = ThisWorkbook.Path
          MsgBox "Working-Directory = " & ThisWorkbookPath





          share|improve this answer































            -3














            Use these codes and enjoy it.



            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


            And here you can call function:



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





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19824164%2fhow-to-get-current-working-directory-using-vba%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              8 Answers
              8






              active

              oldest

              votes








              8 Answers
              8






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              81














              I've tested this:



              When I open an Excel document D:dbtmptest1.xlsm:




              • CurDir() returns C:Users[username]Documents


              • ActiveWorkbook.Path returns D:dbtmp



              So CurDir() has a system default and can be changed.



              ActiveWorkbook.Path does not change for the same saved Workbook.



              For example, CurDir() changes when you do "File/Save As" command, and select a random directory in the File/Directory selection dialog. Then click on Cancel to skip saving. But CurDir() has already changed to the last selected directory.






              share|improve this answer


























              • When you use Workbooks.Open("file.xls") (without giving a path), does it assume CurDir() as the path?

                – André Chalella
                Jul 9 '15 at 9:38






              • 3





                For developers using MS Access: Application.CurrentProject.Path

                – duckboy81
                Feb 13 '17 at 4:06


















              81














              I've tested this:



              When I open an Excel document D:dbtmptest1.xlsm:




              • CurDir() returns C:Users[username]Documents


              • ActiveWorkbook.Path returns D:dbtmp



              So CurDir() has a system default and can be changed.



              ActiveWorkbook.Path does not change for the same saved Workbook.



              For example, CurDir() changes when you do "File/Save As" command, and select a random directory in the File/Directory selection dialog. Then click on Cancel to skip saving. But CurDir() has already changed to the last selected directory.






              share|improve this answer


























              • When you use Workbooks.Open("file.xls") (without giving a path), does it assume CurDir() as the path?

                – André Chalella
                Jul 9 '15 at 9:38






              • 3





                For developers using MS Access: Application.CurrentProject.Path

                – duckboy81
                Feb 13 '17 at 4:06
















              81












              81








              81







              I've tested this:



              When I open an Excel document D:dbtmptest1.xlsm:




              • CurDir() returns C:Users[username]Documents


              • ActiveWorkbook.Path returns D:dbtmp



              So CurDir() has a system default and can be changed.



              ActiveWorkbook.Path does not change for the same saved Workbook.



              For example, CurDir() changes when you do "File/Save As" command, and select a random directory in the File/Directory selection dialog. Then click on Cancel to skip saving. But CurDir() has already changed to the last selected directory.






              share|improve this answer















              I've tested this:



              When I open an Excel document D:dbtmptest1.xlsm:




              • CurDir() returns C:Users[username]Documents


              • ActiveWorkbook.Path returns D:dbtmp



              So CurDir() has a system default and can be changed.



              ActiveWorkbook.Path does not change for the same saved Workbook.



              For example, CurDir() changes when you do "File/Save As" command, and select a random directory in the File/Directory selection dialog. Then click on Cancel to skip saving. But CurDir() has already changed to the last selected directory.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 7 '13 at 8:02









              Jean-François Corbett

              28.7k22110160




              28.7k22110160










              answered Nov 6 '13 at 22:47









              jacouhjacouh

              5,32732033




              5,32732033













              • When you use Workbooks.Open("file.xls") (without giving a path), does it assume CurDir() as the path?

                – André Chalella
                Jul 9 '15 at 9:38






              • 3





                For developers using MS Access: Application.CurrentProject.Path

                – duckboy81
                Feb 13 '17 at 4:06





















              • When you use Workbooks.Open("file.xls") (without giving a path), does it assume CurDir() as the path?

                – André Chalella
                Jul 9 '15 at 9:38






              • 3





                For developers using MS Access: Application.CurrentProject.Path

                – duckboy81
                Feb 13 '17 at 4:06



















              When you use Workbooks.Open("file.xls") (without giving a path), does it assume CurDir() as the path?

              – André Chalella
              Jul 9 '15 at 9:38





              When you use Workbooks.Open("file.xls") (without giving a path), does it assume CurDir() as the path?

              – André Chalella
              Jul 9 '15 at 9:38




              3




              3





              For developers using MS Access: Application.CurrentProject.Path

              – duckboy81
              Feb 13 '17 at 4:06







              For developers using MS Access: Application.CurrentProject.Path

              – duckboy81
              Feb 13 '17 at 4:06















              8














              You have several options depending on what you're looking for.
              Workbook.Path returns the path of a saved workbook. Application.Path returns the path to the Excel executable. CurDir returns the current working path, this probably defaults to your My Documents folder or similar.



              You can also use the windows scripting shell object's .CurrentDirectory property.



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


              But that should get the same result as just



              Debug.Print CurDir





              share|improve this answer




























                8














                You have several options depending on what you're looking for.
                Workbook.Path returns the path of a saved workbook. Application.Path returns the path to the Excel executable. CurDir returns the current working path, this probably defaults to your My Documents folder or similar.



                You can also use the windows scripting shell object's .CurrentDirectory property.



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


                But that should get the same result as just



                Debug.Print CurDir





                share|improve this answer


























                  8












                  8








                  8







                  You have several options depending on what you're looking for.
                  Workbook.Path returns the path of a saved workbook. Application.Path returns the path to the Excel executable. CurDir returns the current working path, this probably defaults to your My Documents folder or similar.



                  You can also use the windows scripting shell object's .CurrentDirectory property.



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


                  But that should get the same result as just



                  Debug.Print CurDir





                  share|improve this answer













                  You have several options depending on what you're looking for.
                  Workbook.Path returns the path of a saved workbook. Application.Path returns the path to the Excel executable. CurDir returns the current working path, this probably defaults to your My Documents folder or similar.



                  You can also use the windows scripting shell object's .CurrentDirectory property.



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


                  But that should get the same result as just



                  Debug.Print CurDir






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 6 '13 at 22:35









                  AndASMAndASM

                  4,43711330




                  4,43711330























                      7














                      It would seem likely that the ActiveWorkbook has not been saved...



                      Try CurDir() instead.






                      share|improve this answer
























                      • CurDir() returns user's profile directory

                        – Ullan
                        Nov 6 '13 at 22:46











                      • That is usually correct, unless Excel has been opened with an explicitly set alternative working directory. If the active workbook has not been saved, then it is entirely up to you as to where it is to be saved.

                        – Monty Wild
                        Nov 6 '13 at 22:48


















                      7














                      It would seem likely that the ActiveWorkbook has not been saved...



                      Try CurDir() instead.






                      share|improve this answer
























                      • CurDir() returns user's profile directory

                        – Ullan
                        Nov 6 '13 at 22:46











                      • That is usually correct, unless Excel has been opened with an explicitly set alternative working directory. If the active workbook has not been saved, then it is entirely up to you as to where it is to be saved.

                        – Monty Wild
                        Nov 6 '13 at 22:48
















                      7












                      7








                      7







                      It would seem likely that the ActiveWorkbook has not been saved...



                      Try CurDir() instead.






                      share|improve this answer













                      It would seem likely that the ActiveWorkbook has not been saved...



                      Try CurDir() instead.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 6 '13 at 22:35









                      Monty WildMonty Wild

                      3,67611529




                      3,67611529













                      • CurDir() returns user's profile directory

                        – Ullan
                        Nov 6 '13 at 22:46











                      • That is usually correct, unless Excel has been opened with an explicitly set alternative working directory. If the active workbook has not been saved, then it is entirely up to you as to where it is to be saved.

                        – Monty Wild
                        Nov 6 '13 at 22:48





















                      • CurDir() returns user's profile directory

                        – Ullan
                        Nov 6 '13 at 22:46











                      • That is usually correct, unless Excel has been opened with an explicitly set alternative working directory. If the active workbook has not been saved, then it is entirely up to you as to where it is to be saved.

                        – Monty Wild
                        Nov 6 '13 at 22:48



















                      CurDir() returns user's profile directory

                      – Ullan
                      Nov 6 '13 at 22:46





                      CurDir() returns user's profile directory

                      – Ullan
                      Nov 6 '13 at 22:46













                      That is usually correct, unless Excel has been opened with an explicitly set alternative working directory. If the active workbook has not been saved, then it is entirely up to you as to where it is to be saved.

                      – Monty Wild
                      Nov 6 '13 at 22:48







                      That is usually correct, unless Excel has been opened with an explicitly set alternative working directory. If the active workbook has not been saved, then it is entirely up to you as to where it is to be saved.

                      – Monty Wild
                      Nov 6 '13 at 22:48













                      4














                      Your code: path = ActiveWorkbook.Path



                      returns blank because you haven't saved your workbook yet.



                      To overcome your problem, go back to the Excel sheet, save your sheet, and run your code again.



                      This time it will not show blank, but will show you the path where it is located (current folder)



                      I hope that helped.






                      share|improve this answer




























                        4














                        Your code: path = ActiveWorkbook.Path



                        returns blank because you haven't saved your workbook yet.



                        To overcome your problem, go back to the Excel sheet, save your sheet, and run your code again.



                        This time it will not show blank, but will show you the path where it is located (current folder)



                        I hope that helped.






                        share|improve this answer


























                          4












                          4








                          4







                          Your code: path = ActiveWorkbook.Path



                          returns blank because you haven't saved your workbook yet.



                          To overcome your problem, go back to the Excel sheet, save your sheet, and run your code again.



                          This time it will not show blank, but will show you the path where it is located (current folder)



                          I hope that helped.






                          share|improve this answer













                          Your code: path = ActiveWorkbook.Path



                          returns blank because you haven't saved your workbook yet.



                          To overcome your problem, go back to the Excel sheet, save your sheet, and run your code again.



                          This time it will not show blank, but will show you the path where it is located (current folder)



                          I hope that helped.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jun 12 '16 at 15:09









                          Mohamed TahirMohamed Tahir

                          764




                          764























                              1














                              Use Application.ActiveWorkbook.Path for just the path itself (without the workbook name) or Application.ActiveWorkbook.FullName for the path with the workbook name.






                              share|improve this answer




























                                1














                                Use Application.ActiveWorkbook.Path for just the path itself (without the workbook name) or Application.ActiveWorkbook.FullName for the path with the workbook name.






                                share|improve this answer


























                                  1












                                  1








                                  1







                                  Use Application.ActiveWorkbook.Path for just the path itself (without the workbook name) or Application.ActiveWorkbook.FullName for the path with the workbook name.






                                  share|improve this answer













                                  Use Application.ActiveWorkbook.Path for just the path itself (without the workbook name) or Application.ActiveWorkbook.FullName for the path with the workbook name.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Nov 9 '15 at 8:06









                                  Agus Sapurta SijabatAgus Sapurta Sijabat

                                  331210




                                  331210























                                      0














                                      This is the VBA that I use to open the current path in an Explorer window:



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




                                      Microsoft Documentation:





                                      • CurDir Function


                                      • Environ Function

                                      • Shell Function






                                      share|improve this answer




























                                        0














                                        This is the VBA that I use to open the current path in an Explorer window:



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




                                        Microsoft Documentation:





                                        • CurDir Function


                                        • Environ Function

                                        • Shell Function






                                        share|improve this answer


























                                          0












                                          0








                                          0







                                          This is the VBA that I use to open the current path in an Explorer window:



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




                                          Microsoft Documentation:





                                          • CurDir Function


                                          • Environ Function

                                          • Shell Function






                                          share|improve this answer













                                          This is the VBA that I use to open the current path in an Explorer window:



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




                                          Microsoft Documentation:





                                          • CurDir Function


                                          • Environ Function

                                          • Shell Function







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Nov 18 '18 at 7:55









                                          ashleedawgashleedawg

                                          12.7k42351




                                          12.7k42351























                                              0














                                              If you really mean pure working Directory, this should suit for you.



                                              Solution 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


                                              But if don't, this should be enough.



                                              Solution B:



                                              Dim ThisWorkbookPath As String

                                              ThisWorkbookPath = ThisWorkbook.Path
                                              MsgBox "Working-Directory = " & ThisWorkbookPath





                                              share|improve this answer




























                                                0














                                                If you really mean pure working Directory, this should suit for you.



                                                Solution 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


                                                But if don't, this should be enough.



                                                Solution B:



                                                Dim ThisWorkbookPath As String

                                                ThisWorkbookPath = ThisWorkbook.Path
                                                MsgBox "Working-Directory = " & ThisWorkbookPath





                                                share|improve this answer


























                                                  0












                                                  0








                                                  0







                                                  If you really mean pure working Directory, this should suit for you.



                                                  Solution 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


                                                  But if don't, this should be enough.



                                                  Solution B:



                                                  Dim ThisWorkbookPath As String

                                                  ThisWorkbookPath = ThisWorkbook.Path
                                                  MsgBox "Working-Directory = " & ThisWorkbookPath





                                                  share|improve this answer













                                                  If you really mean pure working Directory, this should suit for you.



                                                  Solution 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


                                                  But if don't, this should be enough.



                                                  Solution B:



                                                  Dim ThisWorkbookPath As String

                                                  ThisWorkbookPath = ThisWorkbook.Path
                                                  MsgBox "Working-Directory = " & ThisWorkbookPath






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Nov 27 '18 at 4:45









                                                  NOTSermsakNOTSermsak

                                                  156156




                                                  156156























                                                      -3














                                                      Use these codes and enjoy it.



                                                      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


                                                      And here you can call function:



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





                                                      share|improve this answer




























                                                        -3














                                                        Use these codes and enjoy it.



                                                        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


                                                        And here you can call function:



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





                                                        share|improve this answer


























                                                          -3












                                                          -3








                                                          -3







                                                          Use these codes and enjoy it.



                                                          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


                                                          And here you can call function:



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





                                                          share|improve this answer













                                                          Use these codes and enjoy it.



                                                          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


                                                          And here you can call function:



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






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Dec 1 '14 at 8:44









                                                          josefjosef

                                                          29625




                                                          29625






























                                                              draft saved

                                                              draft discarded




















































                                                              Thanks for contributing an answer to Stack Overflow!


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid



                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.


                                                              To learn more, see our tips on writing great answers.




                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function () {
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19824164%2fhow-to-get-current-working-directory-using-vba%23new-answer', 'question_page');
                                                              }
                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              Popular posts from this blog

                                                              Lallio

                                                              Unable to find Lightning Node

                                                              Futebolista