FileDiaglog OK or CANCEL

vman21

Registered User.
Local time
Today, 02:03
Joined
Feb 25, 2014
Messages
18
Greetings. How do you write the code to execute something when OK or CANCEL button is clicked on a fileDialog?

Code:
Set f = Application.FileDialog(3)
f.AllowMultiSelect = False
If f.Show Then
    For i = 1 To f.SelectedItems.Count
        sFile = Filename(f.SelectedItems(i), sPath)
    Next
End If

Code:
Public Function Filename(ByVal strPath As String, sPath) As String
sPath = Left(strPath, InStrRev(strPath, "\"))
Filename = Mid(strPath, InStrRev(strPath, "\") + 1)
End Function
 
You could do something like this:

Code:
Set f = Application.FileDialog(3)
f.AllowMultiSelect = False
If f.Show Then
    For i = 1 To f.SelectedItems.Count
        sfile = f.SelectedItems(i)
    Next
End If

If sfile <> "" Then
'Run code if a file has been selcted
    sfile = FileName(f.SelectedItems(i), sPath)
End If
 
Just wondering why would you use a For loop here? You have declared to disallow multiselect, so using a For is just pointless really. Could you just not get .SelectedItems(1)?
 

Users who are viewing this thread

Back
Top Bottom