Link to home
Start Free TrialLog in
Avatar of Sootah
SootahFlag for United States of America

asked on

Programming "Cancel" button functionality for CommonDialog open

Mkay, currently I'm handling whether or not they press the cancel button with the following line of code:

If CommonDialog1.FileName <> "" Then

But, as it turns out, the commondialog will return the filename even if cancel is pressed. If I just let the thing open and then click cancel it works fine. If I click browse and then select a file and click cancel it'll open it anyway. I had figured if you canceled it wouldn't pass back the .filename.

How do I detect which button was pressed?
Avatar of Amro Osama
Amro Osama
Flag of Egypt image

well ...I don't know how to do that ...
but at my side I reset the Filename Property before I Show the Dialog

CommonDialog1.FileName=""
CommonDialog1.ShowOpen

this Works fine with me ..
OHDev
Setting the property CommonDialog1.CancelError = True means taht an error is generated ig the CancelButton is used.

Private Sub Command1_Click()
Dim strFile As String
On Error GoTo Command1_ClickError
CommonDialog1.CancelError = True
CommonDialog1.ShowOpen
strFile = CommonDialog1.FileName
Exit Sub
Command1_ClickError:
Debug.Print Err.Number
strFile = ""
End Sub

ASKER CERTIFIED SOLUTION
Avatar of fds_fatboy
fds_fatboy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fds_fatboy
fds_fatboy

Bah! beaten to it...
...I almost forgot, there is a system constant for 32755:

cdlCancel.

So check for Error.Number after the error is raised. If this it is cdlCancel, cancel has been pressed otherwise it is a "real" error...