How to deter the <Cancel> from MS Folder Dialog box

rickyfong

Registered User.
Local time
Today, 05:00
Joined
Nov 25, 2010
Messages
199
Code:
Function dialogFolderBrowse() As String
Dim fp As FileDialog
Dim vrtSelectedItem As Variant
Dim VarX As String
 
'this next line is an optional warning to the user so they know what is about to happen
'MsgBox "LOCATE THE DESIRED FOLDER!!", vbOKOnly
 
'Create a FileDialog object as a Folder Picker dialog box.
Set fp = Application.FileDialog(msoFileDialogFolderPicker) ' can use msoFileDialogFilePicker for filenames
fp.AllowMultiSelect = True
If fp.Show = -1 Then
     For Each vrtSelectedItem In fp.SelectedItems
          VarX = vrtSelectedItem
                'vrtSelectedItem is a String that contains the path of each selected item.
 
     Next vrtSelectedItem
 
End If
 
dialogFolderBrowse = VarX
End Function

THE UPPER code is to locate the user desired folder path and name in order to do further process. However, my problem is that no matter I press <OK> or <Cancel> option in the normal MS Folder dialog box, the process still going on. Is that I miss some on error or on exit code??
Thanks a lot!! My idea is that when user press <OK>, the process goes on and when press <Cancel>, no process will be done!!
 
Try by replacing:
Code:
If fp.Show = -1 Then
with this.
Code:
    If fp.SelectedItems.Count > 0 Then
 
THANKS! ANOTHER PROBLEM, WHEN I put the following code in the module, even I have press the <CANCEL> key. The dialog box still appear once more. So, every time, the dialog box appeared twice, and I also need to press the <CANCEL> key twice before I cancel the action. Any idea??

Me.COUNT = fp.Show

of which Me.COUNT is a form variable for temporary storage purpose.
Thanks a lot!!
 
..
of which Me.COUNT is a form variable for temporary storage purpose.
..
For storing what?
Each time you've the fp.Show in your code the file dialog box appears.

By the way did you read my signature?:)
 

Users who are viewing this thread

Back
Top Bottom