What is generally considered to be the best way to store variables like
a user's default save directory?
The replies in here are all good, and directly answer what you are asking.
Here is an alternative train of thought though. I like to have my default
save-as location specified in one of two ways:
1. The Working Directory property of your application's shortcut that the
user uses to launch your application
2. The user's "My Documents" folder.
If you like Option 2, here's how to get the user's path to *ANY* of the
system special folders:
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''
' STDLIB.H
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''
Private Const MAX_PATH As Long = 260
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''
' SHLOBJ.H
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''
' Retrieves the path of a special folder, identified by its CSIDL
Private Declare Function apiSHGetSpecialFolderPath _
Lib "Shell32.dll" Alias "SHGetSpecialFolderPathA" ( _
ByVal hwndOwner As Long, _
ByVal lpszPath As String, _
ByVal nFolder As Long, _
ByVal fCreate As Integer _
) As Integer
Public Enum CSIDL
Desktop = &H0 ' <Desktop>
Internet = &H1 ' Internet Explorer (icon on desktop)
Programs = &H2 ' Start Menu\Programs
Controls = &H3 ' My Computer\Control Panel
Printers = &H4 ' My Computer\Printers
Personal = &H5 ' My Documents
Favorites = &H6 ' <Username>\Favorites
Startup = &H7 ' Start Menu\Programs\Startup
Recent = &H8 ' <Username>\Recent
SendTo = &H9 ' <Username\SendTo
BitBucket = &HA ' <Desktop>\Recycle Bin
StartMenu = &HB ' <Username>\Start Menu
MyDocuments = &HC ' Logical "My Documents" desktop icon
MyMusic = &HD ' "My Music" folder
MyVideo = &HE ' "My Videos" folder
DesktopDirectory = &H10 ' <Username>\Desktop
Drives = &H11 ' My Computer
Network = &H12 ' Network Neighborhood (My Network
Places)
NetHood = &H13 ' <Username>\nethood
Fonts = &H14 ' Windows\Fonts
Templates = &H15 '
CommonStartMenu = &H16 ' All Users\Start Menu
CommonPrograms = &H17 ' All Users\Start Menu\Programs
CommonStartup = &H18 ' All Users\Startup
CommonDesktopDirectory = &H19 ' All Users\Desktop
AppData = &H1A ' <Username>\Application Data
PrintHood = &H1B ' <Username>\PrintHood
LocalAppData = &H1C ' <Username>\Local Settings\Applicaiton
Data (non roaming)
AltStartup = &H1D ' non localized startup
CommonAltStarup = &H1E ' non localized common startup
CommonFavorites = &H1F '
InternetCache = &H20 '
Cookies = &H21 '
History = &H22 '
CommonAppData = &H23 ' All Users\Application Data
Windows = &H24 ' GetWindowsDirectory()
System = &H25 ' GetSystemDirectory()
ProgramFiles = &H26 ' C:\Program Files
MyPictures = &H27 ' C:\Program Files\My Pictures
Profile = &H28 ' C:\Documents and Settings\<Username>
SystemX86 = &H29 ' x86 system directory on RISC
ProgramFilesX86 = &H2A ' x86 C:\Program Files on RISC
ProgramFilesCommon = &H2B ' C:\Program Files\Common
ProgramFilesCommonX86 = &H2C ' x86 Program Files\Common on RISC
CommonTemplates = &H2D ' All Users\Templates
CommonDocuments = &H2E ' All Users\Documents
CommonAdminTools = &H2F ' All Users\Start
Menu\Programs\Administrative Tools
AdminTools = &H30 ' <Username>\Start
Menu\Programs\Administrative Tools
Connections = &H31 ' Network and Dial-up Connections
CommonMusic = &H35 ' All Users\My Music
CommonPictures = &H36 ' All Users\My Pictures
CommonVideo = &H37 ' All Users\My Video
Resources = &H38 ' Resource Direcotry
ResourcesLocalized = &H39 ' Localized Resource Direcotry
CommonOEMLinks = &H3A ' Links to All Users OEM specific apps
CDBurnArea = &H3B ' <Username>\Local Settings\Application
Data\Microsoft\CD Burning
ComputersNearMe = &H3D ' Computers Near Me (computers from
Workgroup membership)
Create = &H8000 ' Combine with CSIDL_ value to force
folder creation
DontVerify = &H4000 ' Combine with CSIDL_ value to return an
unverified folder path
NoAlias = &H1000 ' Combine with CSIDL_ value to insure
non-alias versions of the pidl
PerUserInit = &H800 ' Combine with CSIDL_ value to indicate
per-user init (eg. upgrade)
Mask = &HFF00 ' Mask for all possible flag values
End Enum
Public Function SHGetSpecialFolderPath( _
ByRef ownerForm As Form, _
ByVal specialFolder As CSIDL, _
ByVal forceCreation As Boolean _
) As String
On Error GoTo SHGetSpecialFolderPathError
Dim lpszPath As String
Dim result As Integer
Dim fCreate As Integer
lpszPath = String$(MAX_PATH, 0)
If forceCreation Then fCreate = 1 Else fCreate = 0
result = apiSHGetSpecialFolderPath(ownerForm.hWnd, lpszPath,
specialFolder, fCreate)
If result = 1 Then
' Success
SHGetSpecialFolderPath = Trim$(lpszPath)
Else
SHGetSpecialFolderPath = ""
End If
SHGetSpecialFolderPathExit:
Exit Function
SHGetSpecialFolderPathError:
SHGetSpecialFolderPath = ""
Resume SHGetSpecialFolderPathExit
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''
' Then, in your form, do this:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''
Dim myDocuments As String
myDocuments = SHGetSpecialFolderPath(Me, Personal, False)