Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Cancelling directory dialog box

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,120
Default Cancelling directory dialog box

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
If savePath < "" Then
ActiveWorkbook.SaveAs Filename:= _
savePath & "\" & wkSheet.Name, FileFormat:=xlText _
, CreateBackup:=False
End If

End Sub


--
HTH

Bob Phillips

"Reena" wrote in message
oups.com...
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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cancelling a macro Naive Excel Programming 1 March 16th 05 03:29 PM
Creating a macro that lists directory names within a directory.... Andy Excel Programming 4 November 28th 04 06:13 AM
end Macro after cancelling open dialog No Name Excel Programming 1 June 29th 04 08:58 AM
Show Dialog of File/Directory B Tuohy Excel Programming 6 January 29th 04 01:48 AM
Check if directory empty OR no of files in directory. Michael Beckinsale Excel Programming 2 December 4th 03 10:12 PM


All times are GMT +1. The time now is 11:22 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"