PDA

View Full Version : Solved: Capture Cancel Button



gibbo1715
10-22-2006, 09:53 AM
All

Im using the code below to browse for a folder (I think this came form DrJ)

Function GetFolderPath() As String
Dim oShell As Object
Set oShell = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please Select Location", 0, "c:\\")
If Not oShell Is Nothing Then
GetFolderPath = oShell.Items.Item.Path
Else
GetFolderPath = vbNullString
End If
Set oShell = Nothing
End Function

Can anyone tell me how i can capture when the cancel button is pressed

thanks

gibbo

Jacob Hilderbrand
10-22-2006, 10:04 AM
This is what I use:


Option Explicit

Function BrowseForFolder(Optional Prompt As String, Optional OpenAt As String) As String

Dim ShellApp As Object

Set ShellApp = CreateObject("Shell.Application").BrowseForFolder(0, Prompt, 0, OpenAt)

On Error Resume Next
BrowseForFolder = ShellApp.Self.Path
On Error GoTo 0

Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then
BrowseForFolder = ""
End If
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then
BrowseForFolder = ""
End If
Case Else
BrowseForFolder = ""
End Select

Set ShellApp = Nothing

End Function


This is a modification of the Kb Entry here: http://vbaexpress.com/kb/getarticle.php?kb_id=284

Then after you use the function, check if the value is "" or not.

Norie
10-22-2006, 10:08 AM
gibbo

If cancel is pressed then the function returns vbNullString.
x = GetFolderPath
If x = vbNullString Then
MsgBox "Cancelled"
Else
MsgBox x
End If

gibbo1715
10-22-2006, 10:17 AM
Thanks both, thats what i was after