I wrote the following set of functions to retrieve standard folder locations from the Windows Registry, without having to cut and paste the complex syntax into every procedure where I need to use the Windows | Office | Templates or Documents folder as part of a file path. Each folder path is returned as a string terminated with a backslash.
To use these routines, copy the code into any application where you need to use these paths, and call the function as follows:
Dim strSpecFolder As String strSpecFolder = GetDir Debug.Print strSpecFolder
Here’s the standard routine, which works by using Word’s System property:
Option Compare Database Option Explicit Global objWord As Object Public Function GetDir() As String On Error GoTo WinDirError Set objWord = CreateObject("Word.application") GetDir = _ objWord.System.PrivateProfileString("", _ "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\" & _ "Windows\CurrentVersion\Setup","WinDir") & "\" WinDirExit: objWord.Quit Exit Function WinDirError: MsgBox "Error No: " & Err.Number & _ "; Description: " & Err.Description Resume WinDirExit End Function
The key lies in the second and third parameters passed to the PrivateProfileString method. The parameters used in the preceding routine returns the Windows directory. Table 1 lists the parameters for some other interesting directories.
Table 1. Keys for retrieving directory names from the Windows Registry.
Directory | Second parameter | Third parameter |
Office | HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\8.0 | BinDirPath |
Documents | HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders | Personal |
Templates | HKEY_CURRENT_USER\Software\Microsoft\Office\8.0\Common\FileNew\LocalTemplates |