This is my bas file , I cant seem to set the default !!
Private Const BIF_RETURNONLYFSDIRS = &H1&
Private Const BIF_DONTGOBELOWDOMAIN = &H2&
Private Const BIF_STATUSTEXT = &H4&
Private Const BIF_RETURNFSANCESTORS = &H8&
Private Const BIF_EDITBOX = &H10&
Private Const BIF_VALIDATE = &H20&
Private Const BIF_NEWDIALOGSTYLE = &H40&
Private Const BIF_USENEWUI = BIF_NEWDIALOGSTYLE Or BIF_EDITBOX
Private Const BIF_BROWSEINCLUDEURLS = &H80&
Private Const BIF_UAHINT = &H100&
Private Const BIF_BROWSEFORCOMPUTER = &H1000&
Private Const BIF_BROWSEFORPRINTER = &H2000&
Private Const BIF_BROWSEINCLUDEFILES = &H4000&
Private Const BIF_SHAREABLE = &H8000&
'common to both methods
Private 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
Private Declare Function SHBrowseForFolder Lib _
"shell32.dll" Alias "SHBrowseForFolderA" _
(lpBrowseInfo As BROWSEINFO) As Long
Private Declare Function SHGetPathFromIDList Lib _
"shell32.dll" Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, _
ByVal pszPath As String) As Long
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Sub MoveMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pDest As Any, _
pSource As Any, _
ByVal dwLength As Long)
Private Const MAX_PATH = 260
Private Const WM_USER = &H400
Private Const BFFM_INITIALIZED = 1
'If the lParam parameter is non-zero, enables the
'OK button, or disables it if lParam is zero.
'(docs erroneously said wParam!)
'wParam is ignored and should be set to 0.
'*Private Const BFFM_ENABLEOK As Long = (WM_USER + 101)
'Selects the specified folder. If the wParam
'parameter is FALSE, the lParam parameter is the
'PIDL of the folder to select , or it is the path
'of the folder if wParam is the C value TRUE (or 1).
'Note that after this message is sent, the browse
'dialog receives a subsequent BFFM_SELECTIONCHANGED
'message.
Private Const BFFM_SETSELECTIONA As Long = (WM_USER + 102)
'*Private Const BFFM_SETSELECTIONW As Long = (WM_USER + 103)
'specific to the STRING method
Private Declare Function LocalAlloc Lib "kernel32" _
(ByVal uFlags As Long, _
ByVal uBytes As Long) As Long
Private Declare Function LocalFree Lib "kernel32" _
(ByVal hMem As Long) As Long
Private Const LMEM_FIXED = &H0
Private Const LMEM_ZEROINIT = &H40
Private Const LPTR = (LMEM_FIXED Or LMEM_ZEROINIT)
Public Function BrowseFolder(frm As Form, sTitle As String, Optional bFiles
As Boolean = False) As String
Dim BI As BROWSEINFO
Dim pidl As Long
Dim lpSelPath As Long
Dim sPath As String * MAX_PATH
With BI
.hOwner = frm.hwnd
.pidlRoot = 0
.lpszTitle = sTitle
.lpfn = FARPROC(AddressOf BrowseCallbackProcStr)
If bFiles Then
.ulFlags = BIF_BROWSEINCLUDEFILES Or BIF_STATUSTEXT Or BIF_VALIDATE
Or BIF_DONTGOBELOWDOMAIN
Else
.ulFlags = BIF_RETURNONLYFSDIRS Or BIF_USENEWUI
End If
lpSelPath = LocalAlloc(LPTR, 0)
MoveMemory ByVal lpSelPath, ByVal conEmpStr, 0
.lParam = lpSelPath
End With
pidl = SHBrowseForFolder(BI)
If pidl Then
If SHGetPathFromIDList(pidl, sPath) Then
BrowseFolder = Left$(sPath, InStr(sPath, vbNullChar) - 1)
End If
Call CoTaskMemFree(pidl)
End If
Call LocalFree(lpSelPath)
End Function
Private Function BrowseCallbackProcStr(ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal lParam As Long, _
ByVal lpData As Long) As Long
'Callback for the Browse STRING method.
'On initialization, set the dialog's
'pre-selected folder from the pointer
'to the path allocated as bi.lParam,
'passed back to the callback as lpData param.
Select Case uMsg
Case BFFM_INITIALIZED
Call SendMessage(hwnd, BFFM_SETSELECTIONA, _
True, ByVal lpData)
Case Else:
End Select
End Function
Private Function FARPROC(pfn As Long) As Long
'A dummy procedure that receives and returns
'the value of the AddressOf operator.
'Obtain and set the address of the callback
'This workaround is needed as you can't assign
'AddressOf directly to a member of a user-
'defined type, but you can assign it to another
'long and use that (as returned here)
FARPROC = pfn
End Function
"Don Grover" <dgr...@assoft.com.au> wrote in message
news:uhruJTXvAHA.1108@tkmsftngp03...
lpSelPath = LocalAlloc(LPTR, 0)
This line should allocate enough memory to store the
selected folder string plus the null terminator. You're
allocating zero bytes.
MoveMemory ByVal lpSelPath, ByVal conEmpStr, 0
I don't know where conEmpStr is coming from, but it
should be the selected folder name string. The last param
should be the length of the string and, again you passed zero.
Try this instead:
lpSelPath = LocalAlloc(LPTR, Len(conEmpStr) + 1)
MoveMemory ByVal lpSelPath, ByVal conEmpStr, Len(conEmpStr) + 1
These two lines pass the string to the BI.lParam member, which in turn is
passed to the BrowseCallbackProcStr as lpData.
This should fix the problem. However, I haven't thoroughly gone over the
code for other errors.
Rocky Clark (Kath-Rock Software)
"Don Grover" <dgr...@assoft.com.au> wrote in message
news:uhruJTXvAHA.1108@tkmsftngp03...
Visual Basic Callbacks
Pre-selecting a Folder using the Browse Callback
http://www.mvps.org/vbnet/code/callback/browsecallback.htm
"Don Grover" <dgr...@assoft.com.au> wrote in message
news:uhruJTXvAHA.1108@tkmsftngp03...
"Don Grover" <dgr...@assoft.com.au> wrote in message
news:uhruJTXvAHA.1108@tkmsftngp03...