R
Reena
I found some great code on here recently that gave me a directory
dialog box. I use this to browse to the directory I want my files saved
in, and when I click OK, I assign that path to a variable which I use
later in my macro.
The problem I am having is that when I cancel the dialog box, it does
not return the control to the user. The macro continues and it uses
whatever folder I was in when I cancelled as the path.
Is there some sort of check I need to be doing to make sure the box has
not been cancelled? Here is the code I am using (The top part of this
code came from here, so I dont know anything about it, to be honest.)
My code is in the Sub.
Option Explicit
Enum BrowseForFolderFlags
BIF_RETURNONLYFSDIRS = &H1
BIF_DONTGOBELOWDOMAIN = &H2
BIF_STATUSTEXT = &H4
BIF_BROWSEFORCOMPUTER = &H1000
BIF_BROWSEFORPRINTER = &H2000
BIF_BROWSEINCLUDEFILES = &H4000
BIF_EDITBOX = &H10
BIF_RETURNFSANCESTORS = &H8
End Enum
Private Type BrowseInfo
hwndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Private Declare Function SHBrowseForFolder Lib _
"shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib _
"shell32" (ByVal pidList As Long, _
ByVal lpBuffer As String) As Long
Private Declare Function lstrcat Lib "kernel32" _
Alias "lstrcatA" (ByVal lpString1 As String, _
ByVal lpString2 As String) As Long
Public Function BrowseForFolder(hWnd As Long, _
Optional Title As String, _
Optional Flags As BrowseForFolderFlags) As String
Dim iNull As Integer
Dim IDList As Long
Dim Result As Long
Dim Path As String
Dim bi As BrowseInfo
If Flags = 0 Then Flags = BIF_RETURNONLYFSDIRS
With bi
.lpszTitle = lstrcat(Title, "")
.ulFlags = Flags
End With
IDList = SHBrowseForFolder(bi)
If IDList Then
Path = String$(300, 0)
Result = SHGetPathFromIDList(IDList, Path)
iNull = InStr(Path, vbNullChar)
If iNull Then Path = Left$(Path, iNull - 1)
End If
BrowseForFolder = Path
End Function
Sub SaveWorksheet ()
Dim savePath As String
' Get the path where the file will be saved
savePath = BrowseForFolder(858, "Choose a folder:")
' save the worksheet using worksheet name
ActiveWorkbook.SaveAs Filename:= _
savePath & "\" & wksSheet.Name, FileFormat:=xlText _
, CreateBackup:=False
End Sub
dialog box. I use this to browse to the directory I want my files saved
in, and when I click OK, I assign that path to a variable which I use
later in my macro.
The problem I am having is that when I cancel the dialog box, it does
not return the control to the user. The macro continues and it uses
whatever folder I was in when I cancelled as the path.
Is there some sort of check I need to be doing to make sure the box has
not been cancelled? Here is the code I am using (The top part of this
code came from here, so I dont know anything about it, to be honest.)
My code is in the Sub.
Option Explicit
Enum BrowseForFolderFlags
BIF_RETURNONLYFSDIRS = &H1
BIF_DONTGOBELOWDOMAIN = &H2
BIF_STATUSTEXT = &H4
BIF_BROWSEFORCOMPUTER = &H1000
BIF_BROWSEFORPRINTER = &H2000
BIF_BROWSEINCLUDEFILES = &H4000
BIF_EDITBOX = &H10
BIF_RETURNFSANCESTORS = &H8
End Enum
Private Type BrowseInfo
hwndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Private Declare Function SHBrowseForFolder Lib _
"shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib _
"shell32" (ByVal pidList As Long, _
ByVal lpBuffer As String) As Long
Private Declare Function lstrcat Lib "kernel32" _
Alias "lstrcatA" (ByVal lpString1 As String, _
ByVal lpString2 As String) As Long
Public Function BrowseForFolder(hWnd As Long, _
Optional Title As String, _
Optional Flags As BrowseForFolderFlags) As String
Dim iNull As Integer
Dim IDList As Long
Dim Result As Long
Dim Path As String
Dim bi As BrowseInfo
If Flags = 0 Then Flags = BIF_RETURNONLYFSDIRS
With bi
.lpszTitle = lstrcat(Title, "")
.ulFlags = Flags
End With
IDList = SHBrowseForFolder(bi)
If IDList Then
Path = String$(300, 0)
Result = SHGetPathFromIDList(IDList, Path)
iNull = InStr(Path, vbNullChar)
If iNull Then Path = Left$(Path, iNull - 1)
End If
BrowseForFolder = Path
End Function
Sub SaveWorksheet ()
Dim savePath As String
' Get the path where the file will be saved
savePath = BrowseForFolder(858, "Choose a folder:")
' save the worksheet using worksheet name
ActiveWorkbook.SaveAs Filename:= _
savePath & "\" & wksSheet.Name, FileFormat:=xlText _
, CreateBackup:=False
End Sub