Results 1 to 21 of 21

Thread: ?How can I get the selected folder's name in open common dialog window?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    16

    Unhappy ?How can I get the selected folder's name in open common dialog window?

    Hello all,
    I use a Open Common Dialog so that the user can select multiple folders or/and files. What I want is that when the user clicks (NOT double-clicks) a folder to be able to get the folder's name, because by now all that i can do is get the path of the folder(drive) that contains the selected folder...
    If anyone has any ideea pls help me because I,m stuck...

  2. #2
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: ?How can I get the selected folder's name in open common dialog window?

    I'm not sure of exactly what you are trying to do from your post, but I'm always willing to give an opinion even if in ignorance
    Have you looked at this thread?

    If you are commited to using the commondialog, then you might be able to do something by suclassing it.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    16

    Post Re: ?How can I get the selected folder's name in open common dialog window?

    Thank you Moeur for answering me.
    I'll try to explain better. I'm new in API programming and I don't know how subclassing works but from what I've read some think it's risky (?). Also I don't know that the way I've chosen is the best. What I need to do is this: I want the user to be able to browse through the drives, folders and files of the computer and choose the folders(even drives) and the files that he needs so I can add them (with their complete path) in a list. I thought that the common dialog box is easy to use for the user and he is familiar with it. The problem is that I use VBA from Access 2000 and the common dialog control doesn't exists here... So for short my problem is that when the the user clicks on a folder(not double-clicks ) I cannot retrieve the name of the folder(s) that the user choosed. I can only get the folder's parent path(through CDM_GETFOLDERPATH). For files things work just fine. I renamed the open button and when the user selects files and clicks it I can add them to my list with their ful path. I want to be able to do the same thing with folders.
    Thank you again and hope you can give me a hand on this problem

  4. #4
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: ?How can I get the selected folder's name in open common dialog window?

    The problem is that I use VBA from Access 2000 and the common dialog control doesn't exists here...
    I think it does.
    So, if you are not using the Common Dialog, what are you using?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    16

    Unhappy Re: ?How can I get the selected folder's name in open common dialog window?

    I am not using the common dialog control (the one I understood exists in VB like a text box control or a command button control) and if it does exist do you know how to find it, because I've searched for it and I didn't found any useful control. Also it doesn't exist the drivelist or the folderlist control in VBA so I use the GetOpenFileNameA function from comdlg32.dll and a hook procedure to catch the messages send by the open dialog box...
    I repeat what I need is to get the name of the selected folder. I can tell that the selection has changed through the notification CDN_SELCHANGE but I'm not able to get the name of the selected folder.
    Do you have any ideea how to solve this or should I use something else?
    Thanks and hope you'll write to me soon...

  6. #6
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: ?How can I get the selected folder's name in open common dialog window?

    Did you look at the thread I recommended above?

    I can put a commondialog on my form in VBA. Right-click on the toolbox and check Microsoft commondialog control.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    16

    Re: ?How can I get the selected folder's name in open common dialog window?

    First: I can create using api an open dialog box with all required controls(open, cancel, help buttons, a.s.o) I can browse folders as in any explorer window but I'm not able to retrieve the name of the currently selected folder.
    Second: I've tried to put a Microsoft Common Dialog Control(VB 6) but get one of the errors: "You don't have the licence to use this ActiveX control" or "The OLE server isn't registered". I've tried to register comdlg32.dll using command line and regsvr32 but I get another error saying that the dll is already in memory. I've tried also to add Microsoft Common Dialog Control to my references, succeded but when I try to add a common dialog control to my list I get the same error.
    Thus I have to stick to API and try to figure out a work around for my problem. I want to use common dialog box (and not just an api function browsing for a specific folder) because I want the user to be able to select multiple files and folders the same time and be able to add them(full path and name) to a list in an access form. For files (even multiple selection) works just fine but for the folders I can get the full path and name only by double-clicking them which makes multiple selection impossible. If I just click one or several folders(with CTRL key down) I can get only the path of the folder containing them.
    Example: I multiple select the folowing files: a.txt, b.txt and c.txt from C:\MyFolder the open dialog box automaticaly generates a string containing the names of these files in quotes inside the text-box "File name :". I can then read through API this string and also their path.
    If i multiple select folders I have no clue where to find the equivalent string containing this time the name of the folders in quotes or something like that.

    Thanks again, and hope I'm not to annoying ( or clumsy )

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: ?How can I get the selected folder's name in open common dialog window?

    Sounds like your using the wrong control to get a Folder selection. Shouldnt you be using the
    BrowseForFolder dialog if your only trying to give the user a folder selection method?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    16

    Re: ?How can I get the selected folder's name in open common dialog window?

    I want a window with explorer browsing capabilities that allows me to multiple select files and folders and retrieve their names and full path for further use. In browse for folder dialog can you see the content of a folder(both files and folders) and can you select multiple folders in the same time?
    All I need is that when the user selects a folder to be able to retrieve it's name without opening it. When the user selects a folder the hook procedure gets the CDN_SELCHANGE notification and I want then to get the name of the folder selected...

    Thanks for answering me...

  10. #10
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: ?How can I get the selected folder's name in open common dialog window?

    I think it's time you show us some code of what you've done so far.

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: ?How can I get the selected folder's name in open common dialog window?

    The BrowseForFolder has a flag to include files but I'm not sure about multiple selections. This seems like
    a strange issue. What are you trying to do that needs to give the user the ability to select multiple folder
    and multiple file at the same time?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    16

    Re: ?How can I get the selected folder's name in open common dialog window?

    *******************************

    Function fOFNHookProc( _
    ByVal hwnd As Long, _
    ByVal uiMsg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long) _
    As Long
    Static tofNotify As OFNOTIFY
    Static blnRetVal As Boolean
    Dim hWndParent As Long
    Dim sbuff As String
    Dim bufflen As Long, strlen As Long
    Dim endSlashPos
    Dim sect As String
    Dim startSect
    Dim s As String
    Dim afterSect As String
    Dim leftRowS As String
    Dim rightRowS As String

    hWndParent = apiGetParent(hwnd)
    Select Case uiMsg
    Case WM_NOTIFY
    Call sapiZeroMem(tofNotify, Len(tofNotify))
    Call sapiCopyMem(tofNotify, ByVal lParam, Len(tofNotify))

    Select Case tofNotify.hdr.code
    Case CDN_INITDONE:
    'Debug.Print "CDN_INITDONE"



    hWndParent = apiGetParent(hwnd)
    Call apiSendMessage(hWndParent, CDM_SETCONTROLTEXT, _
    pshHelp, ByVal "Add Folder(s)")
    Call apiSendMessage(hWndParent, CDM_SETCONTROLTEXT, _
    stc4, ByVal "Look in:")

    Call apiSendMessage(hWndParent, CDM_SETCONTROLTEXT, _
    stc2, ByVal "Files of type")

    Call apiSendMessage(hWndParent, CDM_SETCONTROLTEXT, _
    stc3, ByVal "File(s) name(s)")

    Call apiSendMessage(hWndParent, CDM_HIDECONTROL, _
    chx1, ByVal 0&)

    Call apiSendMessage(hWndParent, CDM_SETCONTROLTEXT, _
    IDOK, ByVal "Add File(s)")

    Call apiSendMessage(hWndParent, CDM_SETCONTROLTEXT, _
    IDCANCEL, ByVal "Close")

    Call apiEnumChildWindows(hWndParent, _
    AddressOf fEnumChildProc, 0)

    blnRetVal = True

    Case CDN_SELCHANGE:

    blnRetVal = False

    Case CDN_FOLDERCHANGE:

    blnRetVal = False

    Case CDN_SHAREVIOLATION:
    blnRetVal = False
    Case CDN_HELP:
    blnRetVal = False
    'here should go the code for adding the selected folders to the list
    Case CDN_FILEOK:
    ' here is the code if just one file is selected and for multiple file selection I'll add code later
    sbuff = String$(2 * MAX_LEN + 2, 0)
    strlen = apiSendMessage(hWndParent, CDM_GETFILEPATH, 2 * MAX_LEN, ByVal sbuff)
    If strlen > 0 Then
    sbuff = Left$(sbuff, strlen - 1)
    Form_frmIntro.lsItemToAddToProject.RowSource = _
    Form_frmIntro.lsItemToAddToProject.RowSource + _
    sbuff + ";"
    End If
    blnRetVal = True
    Call apiSetWindowLong(hwnd, DWL_MSGRESULT, 1)

    Case CDN_TYPECHANGE:
    blnRetVal = False

    Case CDN_INCLUDEITEM:
    blnRetVal = False

    End Select
    Case WM_SETREDRAW
    End Select
    fOFNHookProc = blnRetVal
    End Function

    ********************************************************

    Sub sTestCommDlgCallback()
    Dim strFilter As String
    Dim lngRet As Long
    Dim tOFN As tagOPENFILENAME

    strFilter = ahtAddFilterItem(strFilter, "Fisiere .dat (*.dat)", _
    "*.DAT")
    strFilter = ahtAddFilterItem(strFilter, "Fisiere .prn (*.prn)", "*.PRN")
    strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)", "*.TXT")
    strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
    With tOFN
    .hwndOwner = hWndAccessApp
    .lStructSize = Len(tOFN)
    .Flags = OFN_ENABLEHOOK Or ahtOFN_EXPLORER Or OFN_EXPLORER Or OFN_ALLOWMULTISELECT _
    Or OFN_SHOWHELP
    .lpfnHook = fFuncPtr(AddressOf fOFNHookProc)
    .strInitialDir = CurDir
    .hInstance = 0
    .strCustomFilter = String$(255, vbNullChar)
    .nMaxCustFilter = 255
    .strFilter = strFilter
    .nFilterIndex = 1
    .strFile = String$(255, vbNullChar)
    .nMaxFile = 512
    .strFileTitle = String$(255, vbNullChar)
    .nMaxFileTitle = 256
    .strTitle = "Selectare fisiere pentru creare proiect"
    .strDefExt = DefaultExtension
    End With
    lngRet = aht_apiGetOpenFileName(tOFN)
    If lngRet Then Debug.Print _
    Left$(tOFN.strFile, InStr(1, tOFN.strFile, vbNullChar) - 1)
    End Sub

    *********************************************************

    What I want is that when the hook procedure gets the WM_NOTIFY message with CDN_SELCHANGE notification to be able to get the name of the folder selected (because I can get the path to it by calling apiSendMessage(hWndParent, CDM_GETFOLDERPATH, 2 * MAX_LEN, ByVal sbuff)) in order to add it to a list in another Access form...

    Hope now I made it more clear. If you can think of another easiest way to do that I'm opened to suggestions...

    Thanks again for your help!

  13. #13
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: ?How can I get the selected folder's name in open common dialog window?

    Good, I see what you are doing now.
    Let me ask a question or two.
    When you send a CDM_GETFOLDERPATH message, doesn't that give you the name of the directory selected?
    You said you can get the path, but not the name? what is the difference?

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    16

    Re: ?How can I get the selected folder's name in open common dialog window?

    Quote Originally Posted by moeur
    When you send a CDM_GETFOLDERPATH message, doesn't that give you the name of the directory selected?
    You said you can get the path, but not the name? what is the difference?
    No it doesn't. For example if I am browsing inside C:\MyFolder which contains the folders "Folder1" and "Folder2" and the files "File1.txt" and "File2.txt" then:
    - when I click on "Folder1" and I send the CDM_GETFOLDERPATH message what I get is "C:\MyFolder" because this dialog box is file oriented and unfortunately not folder oriented as well(CDM_GETFILEPATH message has the same effect)
    - when I click on "File1.txt" and and I send the CDM_GETFOLDERPATH message what I get is "C:\MyFolder" and when I send CDM_GETFILEPATH message what I get is "C:\MyFolder\File1.txt"
    Do you see now my headache ?
    All I can think of is how to get hold of the selected item of the control (i think is named lst1 -dlgs.h-) containing the list of files and folders in the current folder.
    Thanks again and hope i here from you soon

  15. #15
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: ?How can I get the selected folder's name in open common dialog window?

    I assume you tried CDM_GETSPEC and that didn't help.
    The only other things I can think of right now,
    1. see if you can get VB6's DirListBox, FileListBox and DriveListBox controls into your VBA App. This way you could build your own dialog box.
    2. Use the BrowseForFolder dialog box from the link I mentioned earlier. This will be more difficult in that you will have to subclass it and decipher its listview messages.
    3. There is an excellent example of shell programming that shows you how to build your own VB Explorer. It is Brad Martinez' VBExplorer
    It's fairly complicated and will require a bit of work to extract only the elements you want. to use.

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    16

    Re: ?How can I get the selected folder's name in open common dialog window?

    While digging for answers related to my problem, I came accross this article (folder path in VB.Net ) and here the author said:
    "The accompanying code sample only responds to one such notification, CDN_SELCHANGE, which tells you that the user has selected an item (file or folder) inside the common dialog. When you receive this notification, you can ask the common dialog for the full path of the selected item, by sending it the CDM_GETFILEPATH message, like this:

    StringBuilder pathBuffer = new StringBuilder(_MAX_PATH);
    NativeMethods.SendMessage( hWndParent,
    CommonDlgMessage.GetFilePath,
    _MAX_PATH,
    pathBuffer );
    string path = pathBuffer.ToString();
    "

    I saw it's ment for vb.net and it's C# but as you can see the author stresses that it provides access to both path and folder name. Do you make any sense of it?
    Thanks for the suggestions...

  17. #17
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: ?How can I get the selected folder's name in open common dialog window?

    I have no problem getting the folder from CDN_SELCHANGE notification
    all the following work for me whether a file or folder is selected
    VB Code:
    1. Case CDN_SELCHANGE:
    2.                 hWndParent = apiGetParent(hwnd)
    3.                 Debug.Print "CDN_SELCHANGE: ";
    4.                  sBuff = String$(2 * MAX_LEN + 2, 0)
    5.                 strlen = apiSendMessage(hWndParent, CDM_GETFILEPATH, 2 * MAX_LEN, ByVal sBuff)
    6.                 If strlen > 0 Then _
    7.                     Debug.Print Left(sBuff, strlen - 1)
    8.                
    9.             Case CDN_FOLDERCHANGE:
    10.                 hWndParent = apiGetParent(hwnd)
    11.                 Debug.Print "CDN_FOLDERCHANGE: ";
    12.                  sBuff = String$(2 * MAX_LEN + 2, 0)
    13.                 strlen = apiSendMessage(hWndParent, CDM_GETFILEPATH, 2 * MAX_LEN, ByVal sBuff)
    14.                 If strlen > 0 Then _
    15.                     Debug.Print Left(sBuff, strlen - 1)
    16.                 'can't do that
    17.                 ' blnRetVal = True
    18.                 blnRetVal = False
    19.  
    20.             Case CDN_FILEOK:
    21.                 hWndParent = apiGetParent(hwnd)
    22.                 Debug.Print "CDN_FILEOK: ";
    23.                  sBuff = String$(2 * MAX_LEN + 2, 0)
    24.                 strlen = apiSendMessage(hWndParent, CDM_GETFILEPATH, 2 * MAX_LEN, ByVal sBuff)
    25.                 If strlen > 0 Then _
    26.                     Debug.Print Left(sBuff, strlen - 1)
    27.                 blnRetVal = False
    28.                 'Debug.Print "CDN_FILEOK"

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    16

    Re: ?How can I get the selected folder's name in open common dialog window?

    I tried the code you wrote on my machine and on several others with the same result. All these machines have Windows 2000 (with different service packs) and Office 2000 installed . Do you have Windows XP? Because I don't understand what happens, it's my code wrong or something else I don't know, it just doesn't work. When I select a folder no matter what message I send (CDM_GETFOLDERPATH or CDM_GETFILEPATH ) I get the same result: the path to the folder containing the selected folder (for example in "Look in " I have C:\MyFolder, in listview I have "Fld1", I click on it and all I get is "C:\MyFolder", using either CDM_GETFOLDERPATH or CDM_GETFILEPATH within CDN_SELCHANGE notification ; on your machine can you get "C:\MyFolder\Fld1" without opening "Fld1", just by being in "C:\MyFolder" and selecting(clicking not double-clicking) on "Fld1"? )
    Thanks again for your patience and support

  19. #19
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: ?How can I get the selected folder's name in open common dialog window?

    Do you have Windows XP?
    Yes I am using Windows XP.
    on your machine can you get "C:\MyFolder\Fld1" without opening "Fld1", just by being in "C:\MyFolder" and selecting(clicking not double-clicking) on "Fld1"? )
    This is correct, I just tried it again to make sure I wasn't imagining it.
    Something must be wrong with the 2000 implementation.

  20. #20
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: ?How can I get the selected folder's name in open common dialog window?

    Uh... I usually set the save as flag. So the user saves a file into the folder, then trim off the filename with string manipulation.

    ocx for commondialog can be used in Access, but has to be referenced and has to be registered on all machines using it.

    It's easier to use the api call. I saw one posted above, but I have a txt file attached with my api calls modules incase you are interested. Its based on the main example, and I have added a comment to usefullness (I hope) in it. Most of it is remarked out as I was experimenting with different APIs and its stated in several places that API declarations take up space. So only declare those you are going to use.

    Anyway...

    Hope it helps sorry if I have posted too late or gone off at a tangent...
    Attached Files Attached Files

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    16

    Re: ?How can I get the selected folder's name in open common dialog window?

    I think this thread made its point, but unfortunately for me the solution came from outside, from the saying:"the fastest way to get somewhere is following the road you know", that means that I had to do some wheel reinventing and create my own primitive but working for my machine dialog box...
    Before ending I would like to thank all of you (especialy Moeur for his patience with a newby like me) for your time and advices. I've read everything all of you had written (including attachments) and I think they'll come very handy in my future projects!

Posting Permissions

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



Click Here to Expand Forum to Full Width