malik641
12-08-2005, 05:53 PM
This is code from John Walkenbach's book "Excel 2000 Power Programming with VBA":
Option Explicit
'32-bit API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal _
pszpath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) _
As Long
Public Type BrowseInfo
hOwner As Long
pIDLRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Function GetDirectory(Optional msg) As String
On Error Resume Next
Dim bInfo As BrowseInfo
Dim path As String
Dim r As Long, x As Long, pos As Integer
'Root folder = Desktop
bInfo.pIDLRoot = 0&
'Title in the dialog
If IsMissing(msg) Then
bInfo.lpszTitle = "Please select the folder where 'Old Bio-Analytical MS.xls' exists."
Else
bInfo.lpszTitle = msg
End If
'Type of directory to return
bInfo.ulFlags = &H1
'Display the dialog
x = SHBrowseForFolder(bInfo)
'Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(10))
GetDirectory = Left(path, pos - 1)
Else
GetDirectory = ""
End If
End Function
Sub GetAFolder()
Dim msg As String
msg = "Please select a location for the backup."
Debug.Print GetDirectory(msg)
End Sub
The Sub at the bottom is just a test for the call...which isn't working http://www.vbforums.com/images/smilies/frown.gif
I get the error where the red text is. Apparently variable pos = 0 http://www.vbforums.com/images/smilies/confused.gif
Any ideas????
Option Explicit
'32-bit API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal _
pszpath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) _
As Long
Public Type BrowseInfo
hOwner As Long
pIDLRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Function GetDirectory(Optional msg) As String
On Error Resume Next
Dim bInfo As BrowseInfo
Dim path As String
Dim r As Long, x As Long, pos As Integer
'Root folder = Desktop
bInfo.pIDLRoot = 0&
'Title in the dialog
If IsMissing(msg) Then
bInfo.lpszTitle = "Please select the folder where 'Old Bio-Analytical MS.xls' exists."
Else
bInfo.lpszTitle = msg
End If
'Type of directory to return
bInfo.ulFlags = &H1
'Display the dialog
x = SHBrowseForFolder(bInfo)
'Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(10))
GetDirectory = Left(path, pos - 1)
Else
GetDirectory = ""
End If
End Function
Sub GetAFolder()
Dim msg As String
msg = "Please select a location for the backup."
Debug.Print GetDirectory(msg)
End Sub
The Sub at the bottom is just a test for the call...which isn't working http://www.vbforums.com/images/smilies/frown.gif
I get the error where the red text is. Apparently variable pos = 0 http://www.vbforums.com/images/smilies/confused.gif
Any ideas????