Consulting

Results 1 to 4 of 4

Thread: Solved: Need the Root Directory of Thisworkbook

  1. #1
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location

    Solved: Need the Root Directory of Thisworkbook

    Edit: I apologize for changing the request
    I actually need this to also work if the Root Directory is a UNC Path
    ie: \\MySharedDrive

    I need help getting the Root Directory of Thisworkbook
    [vba]MyDir = Root Directory of ThisWorkbook & "\Blue Prints\"
    'ie: D:\Blue Prints\ or \\MySharedDrive\Blue Prints\
    [/vba] Thanks
    Last edited by frank_m; 02-03-2012 at 12:19 PM.

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [vba]ThisWorkbook.Path[/vba]

    [VBA]Sub t()
    MsgBox GetRootDrive(ThisWorkbook.path)
    End Sub

    Function GetRootDrive(Optional aPath As String) As String
    ' include reference to MS Scripting
    Dim FSO As FileSystemObject, s As String
    Set FSO = New FileSystemObject
    'GetRootFolder = FSO.GetSpecialFolder(WindowsFolder)
    s = FSO.GetDriveName(aPath)
    Set FSO = Nothing
    GetRootDrive = s
    End Function
    [/VBA]

  3. #3
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location
    Thanks Kenneth, that's what I needed to know
    Appreciate your help

    Just adapted to late binding to simplify distribution to the users.
    [vba]
    Sub t()
    MsgBox GetRootDrive(ThisWorkbook.path)
    End Sub

    Function GetRootDrive(Optional aPath As String) As String
    Dim FSO As Object, s As String
    Set FSO = CreateObject("Scripting.FileSystemObject")
    s = FSO.GetDriveName(aPath)
    Set FSO = Nothing
    GetRootDrive = s
    End Function[/vba]

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You can do that in one line Frank

    [vba]

    Function GetRootDrive(Optional aPath As String) As String
    GetRootDrive = CreateObject("Scripting.FileSystemObject").GetDriveName(aPath)
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •