How to get current working directory using vba?
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
add a comment |
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
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 useCurDir()
– Tim Williams
Nov 6 '13 at 22:29
add a comment |
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
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
excel vba
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 useCurDir()
– Tim Williams
Nov 6 '13 at 22:29
add a comment |
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 useCurDir()
– 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
add a comment |
8 Answers
8
active
oldest
votes
I've tested this:
When I open an Excel document D:dbtmptest1.xlsm:
CurDir()returnsC:Users[username]DocumentsActiveWorkbook.PathreturnsD: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.
When you useWorkbooks.Open("file.xls")(without giving a path), does it assumeCurDir()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
add a comment |
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
add a comment |
It would seem likely that the ActiveWorkbook has not been saved...
Try CurDir() instead.
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
add a comment |
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.
add a comment |
Use Application.ActiveWorkbook.Path for just the path itself (without the workbook name) or Application.ActiveWorkbook.FullName for the path with the workbook name.
add a comment |
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:
CurDirFunction
EnvironFunctionShellFunction
add a comment |
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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
I've tested this:
When I open an Excel document D:dbtmptest1.xlsm:
CurDir()returnsC:Users[username]DocumentsActiveWorkbook.PathreturnsD: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.
When you useWorkbooks.Open("file.xls")(without giving a path), does it assumeCurDir()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
add a comment |
I've tested this:
When I open an Excel document D:dbtmptest1.xlsm:
CurDir()returnsC:Users[username]DocumentsActiveWorkbook.PathreturnsD: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.
When you useWorkbooks.Open("file.xls")(without giving a path), does it assumeCurDir()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
add a comment |
I've tested this:
When I open an Excel document D:dbtmptest1.xlsm:
CurDir()returnsC:Users[username]DocumentsActiveWorkbook.PathreturnsD: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.
I've tested this:
When I open an Excel document D:dbtmptest1.xlsm:
CurDir()returnsC:Users[username]DocumentsActiveWorkbook.PathreturnsD: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.
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 useWorkbooks.Open("file.xls")(without giving a path), does it assumeCurDir()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
add a comment |
When you useWorkbooks.Open("file.xls")(without giving a path), does it assumeCurDir()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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 6 '13 at 22:35
AndASMAndASM
4,43711330
4,43711330
add a comment |
add a comment |
It would seem likely that the ActiveWorkbook has not been saved...
Try CurDir() instead.
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
add a comment |
It would seem likely that the ActiveWorkbook has not been saved...
Try CurDir() instead.
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
add a comment |
It would seem likely that the ActiveWorkbook has not been saved...
Try CurDir() instead.
It would seem likely that the ActiveWorkbook has not been saved...
Try CurDir() instead.
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jun 12 '16 at 15:09
Mohamed TahirMohamed Tahir
764
764
add a comment |
add a comment |
Use Application.ActiveWorkbook.Path for just the path itself (without the workbook name) or Application.ActiveWorkbook.FullName for the path with the workbook name.
add a comment |
Use Application.ActiveWorkbook.Path for just the path itself (without the workbook name) or Application.ActiveWorkbook.FullName for the path with the workbook name.
add a comment |
Use Application.ActiveWorkbook.Path for just the path itself (without the workbook name) or Application.ActiveWorkbook.FullName for the path with the workbook name.
Use Application.ActiveWorkbook.Path for just the path itself (without the workbook name) or Application.ActiveWorkbook.FullName for the path with the workbook name.
answered Nov 9 '15 at 8:06
Agus Sapurta SijabatAgus Sapurta Sijabat
331210
331210
add a comment |
add a comment |
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:
CurDirFunction
EnvironFunctionShellFunction
add a comment |
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:
CurDirFunction
EnvironFunctionShellFunction
add a comment |
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:
CurDirFunction
EnvironFunctionShellFunction
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:
CurDirFunction
EnvironFunctionShellFunction
answered Nov 18 '18 at 7:55
ashleedawgashleedawg
12.7k42351
12.7k42351
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 27 '18 at 4:45
NOTSermsakNOTSermsak
156156
156156
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Dec 1 '14 at 8:44
josefjosef
29625
29625
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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