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