Open "My Documenst" with the GetOpenFileNameA API

smig

Registered User.
Local time
Today, 21:09
Joined
Nov 25, 2009
Messages
2,209
how do I set the GetOpenFileNameA function (the windows API OpenFile dialog) to open a start in a system folder like "My Computer", "Desktop", "My Documenst"... ?
I know how to set it to normal filders like "C:\Windows", but not these special foldes.

Thanks,
Tal
 
well, first of all, 'desktop' and 'my documents' have paths on the drive. and they are always user-specific. so for instance, if you're using vista, you can write:
Code:
Environ$("USERPROFILE") & "\Desktop"
to get the current desktop. and do the same for 'my documents', as this is part of the file system too.

I am not sure about 'my computer', as I don't think it has an address that can be viewed by the interface, but you could try using the famous:
Code:
%SystemRoot%\

However, it might be dangerous, as I have not tested it.
 
Thanks,

I know these folders are user specific. if they where not I had no problem finding them :D

Environ$("USERPROFILE") & "\My Documents"
will go to the folder only if it was not moved. how do I go to the correct one ?
 
Environ$("USERPROFILE") & "\My Documents"
will go to the folder only if it was not moved. how do I go to the correct one ?

well I would assume you would employ the same tactic for any other directory that was moved, either by admins or the users. a folder is a folder.

moved where, exactly? by a user via copy paste, right? I would suggest preventing it from happening again by setting permissions on the folder, so your code can always validate its location,.
 
you can move the "My documents" folder (Change it's properties) and the system will still know where it is.

this code will list all available Environment Variables
Code:
Function List_Environment_Variables()
Dim Indx As Integer
Dim EnvString As String
Indx = 1
Do
    EnvString = Environ(Indx)
    MsgBox EnvString
    Indx = Indx + 1
Loop Until EnvString = ""
End Function
 
Last edited:
you can move the "My documents" folder (Change it's properties) and the system will still know where it is.

this code will list all available Environment Variables
Code:
Function List_Environment_Variables()
Dim Indx As Integer
Dim EnvString As String
Indx = 1
Do
    EnvString = Environ(Indx)
    MsgBox EnvString
    Indx = Indx + 1
Loop Until EnvString = ""
End Function

but what does that have to do with your question?
 
Nothing LOL :D

you suggested using the Environ$ function so I did some search and found all the available variables for it. nothing will give the "MyDocuments" though.

But I also found this code that will give the "MyDocuments" location :)
Code:
Option Explicit
Private Type SHITEMID
    cb As Long
    abID As Byte
End Type
Private Type ITEMIDLIST
    mkid As SHITEMID
End Type
Private Const CSIDL_PERSONAL As Long = &H5
Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" _
                        (ByVal hwndOwner As Long, ByVal nFolder As Long, _
                         pidl As ITEMIDLIST) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" _
                        (ByVal pidl As Long, ByVal pszPath As String) As Long
 
Public Function Rep_Documents() As String
    Dim lRet As Long, IDL As ITEMIDLIST, sPath As String
    lRet = SHGetSpecialFolderLocation(100&, CSIDL_PERSONAL, IDL)
    If lRet = 0 Then
        sPath = String$(512, Chr$(0))
        lRet = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath)
        Rep_Documents = Left$(sPath, InStr(sPath, Chr$(0)) - 1)
    Else
        Rep_Documents = vbNullString
    End If
End Function

*** Edit ***
some more search and I found this great list :)
Code:
CSIDL_ADMINTOOLS [B]Windows 2000:[/B] The Administration Tools folder. CSIDL_ALTSTARTUP The non-localized Startup folder. CSIDL_APPDATA The Application Data folder (used to store common program data). CSIDL_BITBUCKET The Recycle Bin on the desktop. CSIDL_COMMON_ADMINTOOLS [B]Windows 2000:[/B] The Administration Tools folder common to all users. CSIDL_COMMON_APPDATA [B]Windows 2000:[/B] The Application Data folder (used to store common program data) common to all users. CSIDL_COMMON_ALTSTARTUP [B]Windos NT, 2000:[/B] The non-localized Startup folder common to all users. CSIDL_COMMON_DESKTOPDIRECTORY [B]Windows NT, 2000:[/B] The Desktop directory (used to store file objects on the Windows desktop) common to all users. CSIDL_COMMON_DOCUMENTS [B]Windows NT, 2000:[/B] The Documents folder common to all users. CSIDL_COMMON_FAVORITES [B]Windows NT, 2000:[/B] The Favorites folder common to all users. CSIDL_COMMON_PROGRAMS [B]Windows NT, 2000:[/B] The Programs folder under the Start Menu common to all users. CSIDL_COMMON_STARTMENU [B]Windows NT, 2000:[/B] The Start Menu folder common to all users. CSIDL_COMMON_STARTUP [B]Windows NT, 2000:[/B] The Startup folder under Start Menu\Programs common to all users. CSIDL_COMMON_TEMPLATES [B]Windows NT, 2000:[/B] The Templates folder common to all users. CSIDL_CONTROLS The Control Panel. CSIDL_COOKIES The folder used for Internet Explorer's cookie list. CSIDL_DESKTOP The Windows desktop. CSIDL_DESKTOPDIRECTORY The Desktop directory (used to hold file objects on the Windows desktop). CSIDL_DRIVES The My Computer folder. CSIDL_FAVORITES The Favorites folder (used primarily to store Internet Explorer's bookmarks). CSIDL_FONTS The Fonts directory (used to hold the fonts installed in Windows). CSIDL_HISTORY The folder used for Internet Explorer's history list. CSIDL_INTERNET The Internet (refering to the Internet Explorer icon on the desktop). CSIDL_INTERNET_CACHE The folder used for Internet Explorer's cache. CSIDL_LOCAL_APPDATA [B]With Internet Explorer 5.0 or later:[/B] Local Application Data folder. CSIDL_MYPICTURES [B]With Internet Explorer 5.0 or later:[/B] The My Pictures folder. CSIDL_NETHOOD The Nethood directory (used to hold objects appearing in Network Neighborhood). CSIDL_NETWORK The Network Neighborhood folder. CSIDL_PERSONAL The My Documents folder. CSIDL_PRINTERS The Printers folder (under My Computer). CSIDL_PRINTHOOD The PrintHood directory (used to store printer links). CSIDL_PROFILE [B]With Internet Explorer 5.0 or later:[/B] The user profile folder. CSIDL_PROGRAM_FILES [B]With Internet Explorer 5.0 or later:[/B] The Program Files folder. CSIDL_PROGRAM_FILES_COMMON [B]Windows NT, 2000:[/B] The Common folder under Program Files. CSIDL_PROGRAM_FILES_COMMONX86 [B]Windows 2000:[/B] The x86 Common folder under Program Files for RISC systems. CSIDL_PROGRAM_FILESX86 [B]Windows 2000:[/B] The x86 Program Files directory on RISC systems. CSIDL_PROGRAMS The Programs folder in the Start Menu. CSIDL_RECENT The Recent folder (used for the Documents list in the Start Menu). CSIDL_SENDTO The Send To folder (used to store Send To menu items). CSIDL_STARTMENU The Start Menu. CSIDL_STARTUP The Startup folder under Start Menu\Programs. CSIDL_SYSTEM [B]With Internet Explorer 5.0 or later:[/B] The Windows System directory. CSIDL_SYSTEMX86 [B]Windows 2000:[/B] The x86 system directory on RISC systems. CSIDL_TEMPLATES The Templates folder (used to store document templates). CSIDL_WINDOWS [B]With Internet Explorer 5.0 or later:[/B] The Windows directory. 
[B]Constant Definitions[/B]


Const CSIDL_FLAG_CREATE = &H8000Const CSIDL_FLAG_DONT_VERIFY = &H4000Const CSIDL_ADMINTOOLS = &H30Const CSIDL_ALTSTARTUP = &H1DConst CSIDL_APPDATA = &H1AConst CSIDL_BITBUCKET = &HAConst CSIDL_COMMON_ADMINTOOLS = &H2FConst CSIDL_COMMON_ALTSTARTUP = &H1DConst CSIDL_COMMON_APPDATA = &H23Const CSIDL_COMMON_DESKTOPDIRECTORY = &H19Const CSIDL_COMMON_DOCUMENTS = &H2EConst CSIDL_COMMON_FAVORITES = &H1FConst CSIDL_COMMON_PROGRAMS = &H17Const CSIDL_COMMON_STARTMENU = &H16Const CSIDL_COMMON_STARTUP = &H18Const CSIDL_COMMON_TEMPLATES = &H2DConst CSIDL_CONTROLS = &H3Const CSIDL_COOKIES = &H21Const CSIDL_DESKTOP = &H0Const CSIDL_DESKTOPDIRECTORY = &H10Const CSIDL_DRIVES = &H11Const CSIDL_FAVORITES = &H6Const CSIDL_FONTS = &H14Const CSIDL_HISTORY = &H22Const CSIDL_INTERNET = &H1Const CSIDL_INTERNET_CACHE = &H20Const CSIDL_LOCAL_APPDATA = &H1CConst CSIDL_MYPICTURES = &H27Const CSIDL_NETHOOD = &H13Const CSIDL_NETWORK = &H12Const CSIDL_PERSONAL = &H5Const CSIDL_PRINTERS = &H4Const CSIDL_PRINTHOOD = &H1BConst CSIDL_PROFILE = &H28Const CSIDL_PROGRAM_FILES = &H26Const CSIDL_PROGRAM_FILES_COMMON = &H2BConst CSIDL_PROGRAM_FILES_COMMONX86 = &H2CConst CSIDL_PROGRAM_FILESX86 = &H2AConst CSIDL_PROGRAMS = &H2Const CSIDL_RECENT = &H8Const CSIDL_SENDTO = &H9Const CSIDL_STARTMENU = &HBConst CSIDL_STARTUP = &H7Const CSIDL_SYSTEM = &H25Const CSIDL_SYSTEMX86 = &H29Const CSIDL_TEMPLATES = &H15Const CSIDL_WINDOWS = &H24
http://www.jasinskionline.com/windowsapi/ref/other/csidls.html
another function will be SHGetSpecialFolderPath, but it will only work with real folders and virtual ones
 
Last edited:
that sure looks like a lot of work, smig, but if it works for you than I'm happy for you! :)
 
After finding the MyDocument thing I kept searching for the full list of CSIDL so we can all benefit from it in future.
 

Users who are viewing this thread

Back
Top Bottom