• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

Disable Cancel or Catch it in system dialogue

Hi,
I am calling the system dialogue to browse, and when I hit cancel i get another system dialogue complaining about long filenames. How can I catch the cancel? Thank you,
d
0
dingo11
Asked:
dingo11
  • 6
  • 3
1 Solution
 
TimCotteeCommented:
Set .CancelError = True.

You can then trap the error

If Err.Number = cdlCancel Then 'cdlCancel = 32755
  'Handle cancel button
End If
0
 
TimCotteeCommented:
Here is an example using the printer dialog but the principle is the same:

Private Sub Command1_Click()
  Dim BeginPage, EndPage, NumCopies, i
  ' Set Cancel to True
  CommonDialog1.CancelError = True
  On Error GoTo ErrHandler
  ' Display the Print dialog box
  CommonDialog1.ShowPrinter
  ' Get user-selected values from the dialog box
  BeginPage = CommonDialog1.FromPage
  EndPage = CommonDialog1.ToPage
  NumCopies = CommonDialog1.Copies
  For i = 1 To NumCopies
    ' Put code here to send data to the printer
  Next i
  Exit Sub
ErrHandler:
  ' User pressed the Cancel button
  Exit Sub
End Sub

0
 
dingo11Author Commented:
Public Function fBrowseForFolder(hwndOwner As Long, sPrompt As String) As String
On Error GoTo ErrHanndler
' Opens the system dialog for browsing for a folder.
'
    Dim iNull    As Integer
    Dim lpIDList As Long
    Dim lResult  As Long
    Dim sPath    As String
    Dim sPath1   As String
    Dim udtBI    As BrowseInfo
    Dim IDL      As ITEMIDLIST

' Get the ID of the folder to use as the root in the directory box. Change the "CSIDL_"
' constant to any of the defined values as shown in the declarations section of this form.

    sPath1 = fGetSpecialFolder(CSIDL_DESKTOP, IDL)

'Initialize Drag & Drop capabilities in the dialog.
    Call OleInitialize(ByVal 0&)
   
    With udtBI
    '    .pIDLRoot = 0  'Display the entire namespace hierarchy
                        'starting with the desktop folder.
       
        .pIDLRoot = IDL.mkid.cb 'Use the desired starting folder.
   
        .hwndOwner = hwndOwner
        .lpszTitle = lstrcat(sPrompt, "")
   
        'Filter for Folders
        .ulFlags = BIF_RETURNONLYFSDIRS Or BIF_USENEWUI
    End With
   
    lpIDList = SHBrowseForFolder(udtBI)
   
    If lpIDList Then
        sPath = String$(MAX_PATH, 0)
        lResult = SHGetPathFromIDList(lpIDList, sPath)
        Call CoTaskMemFree(lpIDList)
       
        iNull = InStr(sPath, vbNullChar)
        If iNull Then sPath = Left$(sPath, iNull - 1)
    End If
   
    Call OleUninitialize
   
    fBrowseForFolder = sPath
ErrHandler:
    MsgBox err.number
End Function
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dingo11Author Commented:
tim,
I am using the api to call the system dialogue,
d
0
 
dingo11Author Commented:
Tim,
0 is the result of a cancel. I tried simply existing the function but only ending it stops it.

    If lResult = 0 Then
        End
    Else
        fBrowseForFolder = sPath
    End If
0
 
TimCotteeCommented:
Rather than end the entire application, perhaps use something like this:

   If lResult = 0 Then
       fBrowseForFolder = "NOT FOUND"
   Else
       fBrowseForFolder = sPath
   End If

Then in your calling procedure:

strFolder = fBrowseForFolder (.....)
If strFolder = "NOT FOUND" Then
 'Warn the user and perhaps retry
End If

0
 
dingo11Author Commented:
Tim,
I can catch it but I cannot seem to get it to exit the function.
d
0
 
dingo11Author Commented:
tim,
Again I wasn't thinking, there is more going on than the function. THe object that called my function is still processing the info which is an empty string. I'll be back,
d
0
 
dingo11Author Commented:
Tim,
The calling procedure was obviously the key. Thanks and have a great day programming,
d
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now