VBA Code to Find My Documents Folder Path

This code will get the folder path directly pointing to the ‘Documents” folder.

Using this code, the documents folder path can be returned in a string variable. This technique is used when You need to save any log or temporary file in specific path.

Here is the code for this.

Sub VBA_Get_Documents_Path()
    'Code From Officetricks
    'Method 1
    'Early Biding - Include Reference to "Windows Script Host Object Model"
    Dim windowsScriptShell As WshShell
    Set windowsScriptShell = New WshShell
    MsgBox windowsScriptShell.SpecialFolders("MyDocuments")
    
    'Method 2
    'Late Biding - During Run Time
    'Dim windowsScriptShell as object
    Set windowsScriptShell = CreateObject("WScript.Shell")
    MsgBox windowsScriptShell.SpecialFolders("Desktop")
End Sub

Press F5 to run the code. This will give 2 message boxes.

One with Documents path and other with Desktop path

VBA Code Get System Folder Path

Not only ‘My Documents’ folder, You can also get path to access these folders as well.

  • AllUsersDesktop
  • AllUsersStartMenu
  • AllUsersPrograms
  • AllUsersStartup
  • Desktop
  • Favorites
  • Fonts
  • MyDocuments
  • Programs
  • Recent
  • StartMenu
  • Startup

Pass any of these word as parameter to the Specialfolders function & it will return the path for corresponding folder.

External Reference: You can get more info from this Microsoft reference link.

The personal folder structure and drive details in each computer differs.

If you want to find a standard place to store your temporary files or log files, then this methods will be ery useful. You dont have to create new folders. Just try any of these default system folder.