none
How to get the Windows directory with VBA ? RRS feed

  • Question

  • Hi,

    How can we get the Windows directory with VBA in a 64 bit system?

    Thanks


    • Edited by WLID1966 Thursday, November 3, 2016 12:04 PM
    Thursday, November 3, 2016 11:45 AM

Answers

  • Hi WLID1966,

    I suppose you want to get Windows install folder.
    If so, please use "FileSystemObject".

    Here is a sample:
       
    Private Sub btn_Get_Windows_Folder_Click()
        ' --- get Windows install folder --------------------------
        Dim FSO As Object
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Range("C2").Value = FSO.GetSpecialFolder(0)
        Set FSO = Nothing
        ' --- get special folder ----------------------------------------
        Dim wsh As Object
        Set wsh = CreateObject("WScript.Shell")
        ' ---
        Range("C3").Value = wsh.specialfolders("Desktop")
        Range("C4").Value = wsh.specialfolders("StartMenu")
        Range("C5").Value = wsh.specialfolders("MyDocuments")
        ' ---
        Set wsh = Nothing
    End Sub
    

    Regards,
    Ashidacchi
    • Marked as answer by WLID1966 Friday, November 4, 2016 10:43 AM
    Friday, November 4, 2016 8:12 AM

All replies

  • WLID1966,

    Again, what are you trying to do? There are several directory commands that you can use to navigate Windows. It's been a while since I messed with that so rather than trying to remember all the details, I'll just direct you to the Dir Function in the Object Browser. In particular, for looping through the files in a directory check out the last paragraph in the Remarks for the Dir Function help page.

    Hope this helps.

    John

    Thursday, November 3, 2016 3:16 PM
  • Hi WLID1966,

    I suppose you want to get Windows install folder.
    If so, please use "FileSystemObject".

    Here is a sample:
       
    Private Sub btn_Get_Windows_Folder_Click()
        ' --- get Windows install folder --------------------------
        Dim FSO As Object
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Range("C2").Value = FSO.GetSpecialFolder(0)
        Set FSO = Nothing
        ' --- get special folder ----------------------------------------
        Dim wsh As Object
        Set wsh = CreateObject("WScript.Shell")
        ' ---
        Range("C3").Value = wsh.specialfolders("Desktop")
        Range("C4").Value = wsh.specialfolders("StartMenu")
        Range("C5").Value = wsh.specialfolders("MyDocuments")
        ' ---
        Set wsh = Nothing
    End Sub
    

    Regards,
    Ashidacchi
    • Marked as answer by WLID1966 Friday, November 4, 2016 10:43 AM
    Friday, November 4, 2016 8:12 AM
  • Hi Ashidacchi,

    Thanks a lot! That's exactly what I was looking for!

    Best regards

    Friday, November 4, 2016 10:49 AM