Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Jun 2002
    Location
    Nottingham, Nottinghamshire, United Kingdom
    Posts
    257
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Finding User's My Documents (A2000)

    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?

  2. #2
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Finding User's My Documents (A2000)

    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
    Cocuments 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

  3. #3
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Finding User's My Documents (A2000)

    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.hWndAccessA pp, 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)
    Cocuments and SettingsMarkDMy Documents

    ? GetSpecialFolderLocation(CSIDL_DESKTOPDIRECTORY)
    Cocuments and SettingsMarkDDesktop

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

    ? GetSpecialFolderLocation(CSIDL_MYPICTURES)
    Cocuments 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

  4. #4
    3 Star Lounger
    Join Date
    Jun 2002
    Location
    Nottingham, Nottinghamshire, United Kingdom
    Posts
    257
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Finding User's My Documents (A2000)

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •