20 Replies Latest reply on Apr 15, 2018 7:22 AM by JOHN GEORGE

    How to browse for folders?

    Peter Brinkhuis

      Hi there,

       

      A macro that I'm making for a customer contains a button that let them browse for a folder. Right now this is the dialog that I'm using:

       

      browseForFolderLimited.png

      Now I (aka they ) want to change this to use the fancier, Office style browse dialog:

       

      browseForFolderDetailed.png

      Is this at all possible with VBA? I've found many examples, but most require office or they work for VBA6. In this topic, Keith Rice seems to confirm that it isn't possible with VBA.

        • Re: How to browse for folders?
          Deepak Gupta

          Sorry not possible with VBA as that is default API option. Had been struggling to same with VBA directly but no success.

            • Re: How to browse for folders?
              Peter Brinkhuis

              That's too bad, thanks

                • Re: How to browse for folders?
                  Ivana Kolin

                  you want to get Open or Save filename? Or only folder name?

                    • Re: How to browse for folders?
                      Peter Brinkhuis

                      I'm asking the user to select a folder where I can save my exports to.

                       

                      So far I found no reference to that in the SolidWorks help, only a reference in the EPDM help. EPDM is not used by us though.

                        • Re: How to browse for folders?
                          Ivana Kolin

                          try this

                           

                          Option Explicit
                          
                          Public Declare PtrSafe Function GetSaveFileName Lib "comdlg32.dll" Alias _
                              "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
                          
                          Public Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" Alias _
                              "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
                             
                          Public Const OFN_ALLOWMULTISELECT As Long = &H200
                          Public Const OFN_CREATEPROMPT As Long = &H2000
                          Public Const OFN_ENABLEHOOK As Long = &H20
                          Public Const OFN_ENABLETEMPLATE As Long = &H40
                          Public Const OFN_ENABLETEMPLATEHANDLE As Long = &H80
                          Public Const OFN_EXPLORER As Long = &H80000
                          Public Const OFN_EXTENSIONDIFFERENT As Long = &H400
                          Public Const OFN_FILEMUSTEXIST As Long = &H1000
                          Public Const OFN_HIDEREADONLY As Long = &H4
                          Public Const OFN_LONGNAMES As Long = &H200000
                          Public Const OFN_NOCHANGEDIR As Long = &H8
                          Public Const OFN_NODEREFERENCELINKS As Long = &H100000
                          Public Const OFN_NOLONGNAMES As Long = &H40000
                          Public Const OFN_NONETWORKBUTTON As Long = &H20000
                          Public Const OFN_NOREADONLYRETURN As Long = &H8000& '*see comments
                          
                          Public Const OFN_NOTESTFILECREATE As Long = &H10000
                          Public Const OFN_NOVALIDATE As Long = &H100
                          Public Const OFN_OVERWRITEPROMPT As Long = &H2
                          Public Const OFN_PATHMUSTEXIST As Long = &H800
                          Public Const OFN_READONLY As Long = &H1
                          Public Const OFN_SHAREAWARE As Long = &H4000
                          Public Const OFN_SHAREFALLTHROUGH As Long = 2
                          Public Const OFN_SHAREWARN As Long = 0
                          Public Const OFN_SHARENOWARN As Long = 1
                          Public Const OFN_SHOWHELP As Long = &H10
                          Public Const OFN_ENABLESIZING As Long = &H800000
                          Public Const OFS_MAXPATHNAME As Long = 260
                          
                          Public Type OPENFILENAME
                              lStructSize As Long
                              hwndOwner As LongPtr
                              hInstance As LongPtr
                              lpstrFilter As String
                              lpstrCustomFilter As String
                              nMaxCustFilter As Long
                              nFilterIndex As Long
                              lpstrFile As String
                              nMaxFile As Long
                              lpstrFileTitle As String
                              nMaxFileTitle As Long
                              lpstrInitialDir As String
                              lpstrTitle As String
                              flags As Long
                              nFileOffset As Integer
                              nFileExtension As Integer
                              lpstrDefExt As String
                              lCustData As Long
                              lpfnHook As LongPtr
                              lpTemplateName As String
                          End Type
                          
                          
                          'OFS_FILE_OPEN_FLAGS and OFS_FILE_SAVE_FLAGS below
                          'are mine to save long statements; they're not
                          'a standard Win32 type.
                          Public Const OFS_FILE_OPEN_FLAGS = OFN_EXPLORER Or _
                              OFN_LONGNAMES Or _
                              OFN_CREATEPROMPT Or _
                              OFN_NODEREFERENCELINKS
                          
                          Public Const OFS_FILE_SAVE_FLAGS = OFN_EXPLORER Or _
                              OFN_LONGNAMES Or _
                              OFN_OVERWRITEPROMPT Or _
                              OFN_HIDEREADONLY
                          
                          
                          'windows version constants
                          Private Const VER_PLATFORM_WIN32_NT As Long = 2
                          Private Const OSV_LENGTH As Long = 76
                          Private Const OSVEX_LENGTH As Long = 88
                          Public OSV_VERSION_LENGTH As Long
                          
                          Public Const WM_INITDIALOG As Long = &H110
                          Private Const SW_SHOWNORMAL As Long = 1
                          
                          
                          
                          
                          Public Function BrowseForFileOpen(strTitle As String, myFilter As String, Optional initialDir As String = "") As String
                              Dim i As Integer
                              Dim OpenFile    As OPENFILENAME
                              Dim lReturn     As Long
                          
                              OpenFile.lpstrFilter = myFilter
                              OpenFile.nFilterIndex = 1
                              OpenFile.hwndOwner = 0
                              OpenFile.lpstrFile = String(257, 0)
                          #If VBA7 Then
                              OpenFile.nMaxFile = LenB(OpenFile.lpstrFile) - 1
                              OpenFile.lStructSize = LenB(OpenFile)
                          #Else
                              OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
                              OpenFile.lStructSize = Len(OpenFile)
                          #End If
                              OpenFile.lpstrFileTitle = OpenFile.lpstrFile
                              OpenFile.nMaxFileTitle = OpenFile.nMaxFile
                             
                              If initialDir <> "" Then OpenFile.lpstrInitialDir = Left(initialDir, i - 1)
                              OpenFile.lpstrTitle = strTitle
                              OpenFile.flags = OFS_FILE_OPEN_FLAGS
                              lReturn = GetOpenFileName(OpenFile)
                          
                              If lReturn = 0 Then
                                  BrowseForFileOpen = ""
                              Else
                                  BrowseForFileOpen = Trim(Left(OpenFile.lpstrFile, InStr(1, OpenFile.lpstrFile, vbNullChar) - 1))
                              End If
                          
                          End Function
                          
                          
                          
                          Public Function BrowseForFileSave(strTitle As String, myFilter As String, strInitialFile As String) As String
                              Dim i As Integer
                              Dim OpenFile    As OPENFILENAME
                              Dim lReturn     As Long
                              Dim mySaveFile As String
                          
                              OpenFile.lpstrFilter = myFilter
                              OpenFile.nFilterIndex = 1
                              OpenFile.hwndOwner = 0
                              OpenFile.lpstrFile = String(257, 0)
                          #If VBA7 Then
                              OpenFile.nMaxFile = LenB(OpenFile.lpstrFile) - 1
                              OpenFile.lStructSize = LenB(OpenFile)
                          #Else
                              OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
                              OpenFile.lStructSize = Len(OpenFile)
                          #End If
                              OpenFile.lpstrFileTitle = OpenFile.lpstrFile
                              OpenFile.nMaxFileTitle = OpenFile.nMaxFile
                             
                              If strInitialFile <> "" Then
                                  i = InStrRev(strInitialFile, "\")
                                  If i > 0 Then
                                      OpenFile.lpstrInitialDir = Left(strInitialFile, i - 1)
                                      mySaveFile = Mid(strInitialFile, i + 1)
                                      OpenFile.lpstrFile = mySaveFile & String(257 - Len(mySaveFile), 0)
                                  End If
                              End If
                          
                              OpenFile.lpstrTitle = strTitle
                              OpenFile.flags = OFS_FILE_SAVE_FLAGS
                              lReturn = GetSaveFileName(OpenFile)
                          
                              If lReturn = 0 Then
                                  BrowseForFileSave = ""
                              Else
                                  BrowseForFileSave = Trim(Left(OpenFile.lpstrFile, InStr(1, OpenFile.lpstrFile, vbNullChar) - 1))
                              End If
                          
                          End Function
                          
                          
                          
                          Private Function GetAddress(ByVal Addr As Long) As Long
                              GetAddress = Addr
                          End Function
                          
                          
                          
                          Public Function OFNHookProc(ByVal hwnd As Long, _
                              ByVal uMsg As Long, _
                              ByVal wParam As Long, _
                              ByVal lParam As Long) As Long
                              Select Case uMsg
                                  Case WM_INITDIALOG
                               
                                      'Next line is not needed. Just left here as an example of how to hook the save dialog
                                      'Call SendMessage(hwnd, BFFM_SETSELECTION, 1, ByVal initFile$)
                               
                              End Select
                              OFNHookProc = 0
                          End Function
                          
                          
                          Sub main()
                          
                          
                              Dim myFileName As String
                             
                              myFileName = "test.txt"
                              myFileName = BrowseForFileSave("", "", "C:\export.log")
                            
                          End Sub
                          
                  • Re: How to browse for folders?
                    Amen Allah Jlili

                    I wish I could understand all languages