ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   get the parent folder of the working folder (https://www.excelbanter.com/excel-programming/340107-get-parent-folder-working-folder.html)

tom taol

get the parent folder of the working folder
 
i wonder that get the parent folder of folder of current excel file
using macro.

ex) working directory : c:\aa\bb
answer : c:\aa



*** Sent via Developersdex http://www.developersdex.com ***

PaulD

get the parent folder of the working folder
 
"tom taol" wrote in message
...
: i wonder that get the parent folder of folder of current excel file
: using macro.
:
: ex) working directory : c:\aa\bb
: answer : c:\aa
:
Use the GetParentFolderName Method of the FileSystemObject. See MSDN link
below
http://tinyurl.com/afbgz
Paul D



Bob Phillips[_6_]

get the parent folder of the working folder
 
Sub GetParent()
Dim oFSO As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
MsgBox oFSO.GetFile(ActiveWorkbook.FullName).ParentFolder .ParentFolder.Path
End Sub


--
HTH

Bob Phillips

"tom taol" wrote in message
...
i wonder that get the parent folder of folder of current excel file
using macro.

ex) working directory : c:\aa\bb
answer : c:\aa



*** Sent via Developersdex http://www.developersdex.com ***




NickHK

get the parent folder of the working folder
 
Tom,
How about:
Folders = Split(ThisWorkbook.Path, "\")
MsgBox Folders(UBound(Folders) - 1)

Need error checking in the situation of "C:\MyFile.xls".

NickHK

"tom taol" wrote in message
...
i wonder that get the parent folder of folder of current excel file
using macro.

ex) working directory : c:\aa\bb
answer : c:\aa



*** Sent via Developersdex http://www.developersdex.com ***




Bob Phillips[_6_]

get the parent folder of the working folder
 


"NickHK" wrote in message
...

Need error checking in the situation of "C:\MyFile.xls".


Mine also fails for root files, so also needs error handling.



Dana DeLouis[_3_]

get the parent folder of the working folder
 
Mine also fails for root files, so also needs error handling.

Here's a slight change that may help with root files.

Sub GetParent()
Dim oFSO As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")

With oFSO.GetFile(ActiveWorkbook.FullName).ParentFolder
If .IsRootFolder Then
'No Parent Folder
Debug.Print .Path
Else
Debug.Print .ParentFolder.Path
End If
End With
End Sub

HTH :)
--
Dana DeLouis
Win XP & Office 2003


"Bob Phillips" wrote in message
...


"NickHK" wrote in message
...

Need error checking in the situation of "C:\MyFile.xls".


Mine also fails for root files, so also needs error handling.





Bob Phillips[_6_]

get the parent folder of the working folder
 
I could have (should have?) done that myself, but I guess I am too lazy.

Thanks Dana.

Bob

"Dana DeLouis" wrote in message
...
Mine also fails for root files, so also needs error handling.


Here's a slight change that may help with root files.

Sub GetParent()
Dim oFSO As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")

With oFSO.GetFile(ActiveWorkbook.FullName).ParentFolder
If .IsRootFolder Then
'No Parent Folder
Debug.Print .Path
Else
Debug.Print .ParentFolder.Path
End If
End With
End Sub

HTH :)
--
Dana DeLouis
Win XP & Office 2003


"Bob Phillips" wrote in message
...


"NickHK" wrote in message
...

Need error checking in the situation of "C:\MyFile.xls".


Mine also fails for root files, so also needs error handling.








All times are GMT +1. The time now is 02:53 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com