CommonDialog Cancel button handling in VB6

Posted on 2009-12-23
Last Modified: 2012-05-08
I am using Common Dialog to open the specified directory and give the user an ability to select the file and import it in.
But if I click the Cancel and don't select any file - it errorws out.  I need to know how to handle the Cacel button.
Question by:Aquarus
    LVL 29

    Expert Comment

    You can have it throw and error or you can just ignore cancel and check for a valid filename location.

    If you add this line then an cdlCancel = 32755 (&H7FF3) will be thrown which you could trap.
    CommonDialog1.CancelError = True

    No error will be thrown
    CommonDialog1.CancelError = False
    LVL 16

    Expert Comment

    There are two possible ways to handle this.

    1. Before showing the window, you can set the common dialog property "CanelError" = False.  Then you will have to look at the contents of the return value to determine if the user cancled.

    2. Add an error handler.  Catch Error #32755 to know the user clicked "Cancel"
      On Error Goto CatchError
      'Do stuff with selected File Here
      Exit Sub
      if Err.Number = 32755 then
        'Do stuff here based on the fact user clicked Cancel
        'Some other Error happened - handle it here

    LVL 29

    Accepted Solution

    Forgot to attach the example by using cdlOFNFileMustExist flag it prevents the user from adding bogus information in this case the file must exists.
    Private Sub Command1_Click()
    Dim szFileName As String
    CommonDialog1.CancelError = False
    CommonDialog1.Flags = cdlOFNFileMustExist
    szFileName = CommonDialog1.FileName
    If LenB(szFileName) <> 0 Then
      Debug.Print szFileName
      Debug.Print "Cancel"
    End If
    CommonDialog1.FileName = vbNullString
    End Sub

    Open in new window


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now