• Finding User’s My Documents (A2000)

    Author
    Topic
    #401378

    My users run mostly WinXP with some 98’s. Under XP individual users have their own My Documents which could be anywhere. I use automation to mail merge and have code that opens a standard OpenFile dialog to merge with an existing document, but I want to point it at the user’s own documents

    Is there a way of establishing in code the location of the current user’s root My Documents folder?

    Viewing 3 reply threads
    Author
    Replies
    • #790179

      Easist way to do this (w/o resorting to convoluted API’s) is to use Windows Script Host (WSH), if available. Example:

      Function GetMyDocsFolder() As String

      Dim wsh As New IWshShell_Class
      GetMyDocsFolder = wsh.SpecialFolders(“MyDocuments”)
      Set wsh = Nothing

      End Function

      Function will return full path of current Windows user’s “My Documents” folder:

      ? GetMyDocsFolder
      C:Documents and Settingsmark.d******My Documents

      To use WSH must set reference to following type library:

      Library IWshRuntimeLibrary
      C:WINNTSystem32wshom.ocx
      Windows Script Host Object Model (Ver 1.0)

      This should be available on any current/recent version of Windows (WIN 98 or later).

      HTH

    • #790180

      Easist way to do this (w/o resorting to convoluted API’s) is to use Windows Script Host (WSH), if available. Example:

      Function GetMyDocsFolder() As String

      Dim wsh As New IWshShell_Class
      GetMyDocsFolder = wsh.SpecialFolders(“MyDocuments”)
      Set wsh = Nothing

      End Function

      Function will return full path of current Windows user’s “My Documents” folder:

      ? GetMyDocsFolder
      C:Documents and Settingsmark.d******My Documents

      To use WSH must set reference to following type library:

      Library IWshRuntimeLibrary
      C:WINNTSystem32wshom.ocx
      Windows Script Host Object Model (Ver 1.0)

      This should be available on any current/recent version of Windows (WIN 98 or later).

      HTH

    • #790422

      In further reply, here is example of using Windows SHGetSpecialFolderLocation API function to get a user’s “My Documents” folder path. This may be more reliable than using WSH, if not sure if all users have WSH installed/enabled on their systems, plus you don’t need any external references since the functionality is provided by SHELL32.DLL:

      Option Explicit

      Public Const CSIDL_DESKTOP = &H0
      Public Const CSIDL_PERSONAL = &H5 ‘My Documents
      Public Const CSIDL_FAVORITES = &H6
      Public Const CSIDL_DESKTOPDIRECTORY = &H10
      Public Const CSIDL_WINDOWS = &H24
      Public Const CSIDL_SYSTEM = &H25
      Public Const CSIDL_MYPICTURES = &H27
      Public Const MAX_PATH = 260

      Public Declare Function SHGetPathFromIDList Lib “shell32” _
      Alias “SHGetPathFromIDListA” _
      (ByVal pidl As Long, ByVal pszPath As String) As Long

      Public Declare Function SHGetSpecialFolderLocation Lib “shell32” _
      (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long

      Public Declare Sub CoTaskMemFree Lib “ole32” (ByVal pv As Long)

      Public Function GetSpecialFolderLocation(CSIDL As Long) As String

      Dim strPath As String
      Dim pidl As Long

      If SHGetSpecialFolderLocation(Application.hWndAccessApp, CSIDL, pidl) = 0 Then
      strPath = Space$(MAX_PATH)

      If SHGetPathFromIDList(ByVal pidl, ByVal strPath) Then
      GetSpecialFolderLocation = Left$(strPath, InStr(strPath, Chr$(0)) – 1)
      End If

      CoTaskMemFree pidl
      End If

      End Function

      Examples (WIN XP – will work in older versions of Windows where folder type is applicable):

      ? GetSpecialFolderLocation(CSIDL_PERSONAL)
      C:Documents and SettingsMarkDMy Documents

      ? GetSpecialFolderLocation(CSIDL_DESKTOPDIRECTORY)
      C:Documents and SettingsMarkDDesktop

      ? GetSpecialFolderLocation(CSIDL_WINDOWS)
      C:WINDOWS
      ? GetSpecialFolderLocation(CSIDL_SYSTEM)
      C:WINDOWSSYSTEM32

      ? GetSpecialFolderLocation(CSIDL_MYPICTURES)
      C:Documents and SettingsMarkDMy DocumentsMy Pictures

      There are many more CSIDL constants that can be used to get path for other Windows special folders. For brevity only a few listed above. For more info see MSDN:

      MSDN CSIDL Reference

      Note all values listed are in hexadecimal. Example: CSIDL_PERSONAL (0x0005) is declared in VB as:
      Const CSIDL_PERSONAL = &H5.

      HTH

      • #791282

        Thanks Mark, the second method appears to be the way forward and I will incorporate it over the weekend

      • #791283

        Thanks Mark, the second method appears to be the way forward and I will incorporate it over the weekend

    • #790423

      In further reply, here is example of using Windows SHGetSpecialFolderLocation API function to get a user’s “My Documents” folder path. This may be more reliable than using WSH, if not sure if all users have WSH installed/enabled on their systems, plus you don’t need any external references since the functionality is provided by SHELL32.DLL:

      Option Explicit

      Public Const CSIDL_DESKTOP = &H0
      Public Const CSIDL_PERSONAL = &H5 ‘My Documents
      Public Const CSIDL_FAVORITES = &H6
      Public Const CSIDL_DESKTOPDIRECTORY = &H10
      Public Const CSIDL_WINDOWS = &H24
      Public Const CSIDL_SYSTEM = &H25
      Public Const CSIDL_MYPICTURES = &H27
      Public Const MAX_PATH = 260

      Public Declare Function SHGetPathFromIDList Lib “shell32” _
      Alias “SHGetPathFromIDListA” _
      (ByVal pidl As Long, ByVal pszPath As String) As Long

      Public Declare Function SHGetSpecialFolderLocation Lib “shell32” _
      (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long

      Public Declare Sub CoTaskMemFree Lib “ole32” (ByVal pv As Long)

      Public Function GetSpecialFolderLocation(CSIDL As Long) As String

      Dim strPath As String
      Dim pidl As Long

      If SHGetSpecialFolderLocation(Application.hWndAccessApp, CSIDL, pidl) = 0 Then
      strPath = Space$(MAX_PATH)

      If SHGetPathFromIDList(ByVal pidl, ByVal strPath) Then
      GetSpecialFolderLocation = Left$(strPath, InStr(strPath, Chr$(0)) – 1)
      End If

      CoTaskMemFree pidl
      End If

      End Function

      Examples (WIN XP – will work in older versions of Windows where folder type is applicable):

      ? GetSpecialFolderLocation(CSIDL_PERSONAL)
      C:Documents and SettingsMarkDMy Documents

      ? GetSpecialFolderLocation(CSIDL_DESKTOPDIRECTORY)
      C:Documents and SettingsMarkDDesktop

      ? GetSpecialFolderLocation(CSIDL_WINDOWS)
      C:WINDOWS
      ? GetSpecialFolderLocation(CSIDL_SYSTEM)
      C:WINDOWSSYSTEM32

      ? GetSpecialFolderLocation(CSIDL_MYPICTURES)
      C:Documents and SettingsMarkDMy DocumentsMy Pictures

      There are many more CSIDL constants that can be used to get path for other Windows special folders. For brevity only a few listed above. For more info see MSDN:

      MSDN CSIDL Reference

      Note all values listed are in hexadecimal. Example: CSIDL_PERSONAL (0x0005) is declared in VB as:
      Const CSIDL_PERSONAL = &H5.

      HTH

    Viewing 3 reply threads
    Reply To: Finding User’s My Documents (A2000)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: