+ Reply to Thread
Results 1 to 7 of 7

BrowseForFolder API Makes Excel Crash

Hybrid View

  1. #1
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    BrowseForFolder API Makes Excel Crash

    Hi,

    I am using the bellow API Calls to bring up the windows standard Folder Picker. The Folder Picker DOES appear. However when I select a folder and click its "OK" button, it makes Excel crash.

    Can someone please figure this out as to where I am making mistake, because I myself tried to make it 64-bit compatible by using Ptrsafe keyword and changing the pointers data type from Long to Longptr.

    Question 2: How can we make it show the files of the folders as well? (it currently shows only folder)


    Private Type BROWSEINFO
            hOwner As LongPtr
            pidlRoot As LongPtr
            pszDisplayName As String
            lpszTitle As String
            ulFlags As LongPtr
            lpfn As Long
            lParam As LongPtr
            iImage As LongPtr
    End Type
    'Commonly-used ulFlags constants
    'Only return file system directories.
    'If the user selects folders that are not
    'part of the file system (such as 'My Computer'),
    'the OK button is grayed.
    Private Const BIF_RETURNONLYFSDIRS As Long = &H1
    'Use a newer dialog style, which gives a richer experience
    Private Const BIF_NEWDIALOGSTYLE As Long = &H40
    'Hide the default 'Make New Folder' button
    Private Const BIF_NONEWFOLDERBUTTON As Long = &H200
    'Messages sent from dialog to callback function
    Private Const BFFM_INITIALIZED = 1
    Private Const BFFM_SELCHANGED = 2
    'Messages sent to browser from callback function
    Private Const WM_USER = &H400
    'Set the selected path
    Private Const BFFM_SETSELECTIONA = WM_USER + 102
    
    'Enable/disable the OK button
    Private Const BFFM_ENABLEOK = WM_USER + 101
    'The maximum allowed path
    Private Const MAX_PATH = 260
    'Main Browse for directory function
    Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" _
    Alias "SHBrowseForFolderA" _
    (ByRef lpBrowseInfo As BROWSEINFO) As Long
    'Gets a path from a pidl
    Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" _
    Alias "SHGetPathFromIDListA" _
    (ByVal pidl As Long, _
    ByVal pszPath As String) As Long
    'Used to set the browse dialog's title
    Declare PtrSafe Function SetWindowText Lib "user32" _
    Alias "SetWindowTextA" _
    (ByVal hwnd As Long, _
    ByVal lpString As String) As Long
    'A versions of SendMessage, to send strings to the browser
    Private Declare PtrSafe Function SendMessageString Lib "user32" _
    Alias "SendMessageA" (ByVal hwnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, _
    ByVal lParam As String) As Long
    'Variables to hold the initial options,
    'set in the callback function
    Dim msInitialPath As String
    Dim msTitleBarText As String
    'The main function to initialize and show the dialog
    Function GetDirectory(Optional ByVal sInitDir As String, _
    Optional ByVal sTitle As String, _
    Optional ByVal sMessage As String, _
    Optional ByVal hwndOwner As Long, _
    Optional ByVal bAllowCreateFolder As Boolean) _
    As String
    
    'A variable to hold the UDT
    Dim uInfo As BROWSEINFO
    Dim sPath As String
    Dim lResult As Long
    'Check that the initial directory exists
    On Error Resume Next
    sPath = Dir(sInitDir & "\*.*", vbNormal + vbDirectory)
    If Len(sPath) = 0 Or Err.Number <> 0 Then sInitDir = ""
    On Error GoTo 0
    'Store the initials setting in module-level variables,
    'for use in the callback function
    msInitialPath = sInitDir
    msTitleBarText = sTitle
    'If no owner window given, use the Excel window
    'N.B. Uses the ApphWnd function in MWindows
    If hwndOwner = 0 Then hwndOwner = ApphWnd
    'Initialise the structure to pass to the API function
    With uInfo
    .hOwner = hwndOwner
    .pszDisplayName = String$(MAX_PATH, vbNullChar)
    .lpszTitle = sMessage
    .ulFlags = BIF_RETURNONLYFSDIRS + BIF_NEWDIALOGSTYLE _
    + IIf(bAllowCreateFolder, 0, BIF_NONEWFOLDERBUTTON)
    'Pass the address of the callback function in the UDT
    '.lpfn = LongToLong(AddressOf BrowseCallBack)
    End With
    'Display the dialog, returning the ID of the selection
    lResult = SHBrowseForFolder(uInfo)
    'Get the path string from the ID
    GetDirectory = GetPathFromID(lResult)
    End Function
    'Windows calls this function when the dialog events occur
    'imran
    Private Function BrowseCallBack(ByVal hwnd As LongPtr, _
    ByVal Msg As LongPtr, ByVal lParam As LongPtr, _
    ByVal pData As LongPtr) As LongPtr
    Dim sPath As String
    'This is called by Windows, so don't allow any errors!
    On Error Resume Next
    Select Case Msg
    Case BFFM_INITIALIZED
    'Dialog is being initialized,
    'so set the initial parameters
    'The dialog caption
    If msTitleBarText <> "" Then
    SetWindowText hwnd, msTitleBarText
    End If
    'The initial path to display
    If msInitialPath <> "" Then
    SendMessageString hwnd, BFFM_SETSELECTIONA, 1, _
    msInitialPath
    End If
    Case BFFM_SELCHANGED
    'User selected a folder
    'lParam contains the pidl of the folder, which can be
    'converted to the path using GetPathFromID
    'sPath = GetPathFromID(lParam)
    'We could put extra checks in here,
    'e.g. to check if the folder contains any workbooks,
    'and send the BFFM_ENABLEOK message to enable/disable
    'the OK button:
    'SendMessage hwnd, BFFM_ENABLEOK, 0, True / False
    End Select
    End Function
    'Converts a PIDL to a path string
    Private Function GetPathFromID(ByVal lID As Long) As String
    Dim lResult As Long
    Dim sPath As String * MAX_PATH
    
    lResult = SHGetPathFromIDList(lID, sPath)
    If lResult <> 0 Then
    GetPathFromID = Left$(sPath, InStr(sPath, Chr$(0)) - 1)
    End If
    End Function
    'VBA doesn 't let us assign the result of AddressOf
    'to a variable, but does allow us to pass it to a function.
    'This 'do nothing' function works around that problem
    Private Function LongToLong(ByVal lAddr As Long) As Long
    LongToLong = lAddr
    End Function
    usage:

    Sub BrowseFolder()
    Dim fldr As String
    
    fldr = GetDirectory("A path to sub folder", "Browse")
    End Sub
    This is the line that actually crashed Excel
    lResult = SHGetPathFromIDList(lID, sPath)
    in GetPathFromID function.


    Help would br greatly appreciated.

    Best Regards

    Imran Bhatti
    Attached Files Attached Files
    Last edited by ImranBhatti; 10-13-2021 at 02:10 PM.
    Teach me Excel VBA

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: BrowseForFolder API Makes Excel Crash


    Hi !

    Q2 : not with this folders only browser !

    Why don't you just use the Excel / VBA features ?

  3. #3
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: BrowseForFolder API Makes Excel Crash

    Thanks, Mark! For Answer 2:

    I was just after displaying the Folders in hierarchical (i.e.Treeview) but using a built-in dialogue and not creating my own treeview. But not sure why it is crashing Excel application.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: BrowseForFolder API Makes Excel Crash


    Could be something broken on Windows side …

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: BrowseForFolder API Makes Excel Crash


    As the best seems to use internal Excel / VBA features like the classic FileDialog or even the so easy GetOpenFilename …

    Anyway a demonstration to browse folders only :

    PHP Code: 
    Sub DemoFolder()
        
    Dim obj As ObjectP$
        
    Set obj CreateObject("Shell.Application").BrowseForFolder(0vbLf "Sélection :"1"")
         If 
    obj Is Nothing Then Beep: Exit Sub Else obj.Self.Path "\": Set obj = Nothing
        MsgBox P
    End Sub 

  6. #6
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: BrowseForFolder API Makes Excel Crash

    @sir Marc

    Firstly thank you so much for the shortest code for the outcome that I was after. Currently, I wanted to have a tree view of folders and let the user expand and collapse the hierarchy which is achieved via your code.

    Secondly, is there a parameter for your code to further show the files of these folders?

    Once again thank you so much*1000 (cos you are beyond reputation)

    Best Regards
    Imran Bhatti

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: BrowseForFolder API Makes Excel Crash


    No as it's for folders only …

    But you can use GetOpenFilename or better FileDialog where it seems you can setup the view …

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. This code makes excel crash. Why and how to fix it?
    By jptaz45 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-03-2020, 11:38 AM
  2. Userform makes excel crash - Run-time error -2147417848 (80010108)
    By jptaz45 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-27-2020, 03:23 PM
  3. Power Query makes Excel crash - can't access Data Model
    By phil34 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-10-2018, 01:01 PM
  4. [SOLVED] Excel 2007 crash: Just open the file, do anything to it, save, close, open againg = CRASH
    By Alex Piotto in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-27-2018, 07:53 AM
  5. Replies: 0
    Last Post: 05-01-2014, 05:46 PM
  6. BeforeSave code makes excel crash every time
    By keb in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-05-2011, 03:04 PM
  7. XLA makes XL2003 crash
    By Soren in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-26-2005, 05:05 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1