That code is verbatim from my web site
(
www.cpearson.com/Excel/BrowseFolder.htm), and it works for me. I just tried
it in both Excel 2003 and Excel 2007 on both Windows XP Pro SP2 and Windows
Vista Ultimate. It works fine. Put your cursor inside the OpenMyBrowser sub
and then press the F8 key to step through the code line by line. Does the
browse folder dialog display? Do you get an error?
Just for diagnostics, change
ID = SHBrowseForFolderA(BrowseInfo)
to
ID = SHBrowseForFolderA(BrowseInfo)
MsgBox "Last DLL Error: " & CStr(Err.LastDLLError)
If the MsgBox displays anything but 0, then the call to SHBrowseForFolderA
failed and you can decode the Err.LastDLLError using other code on my web
site (
http://www.cpearson.com/Excel/FormatMessage.aspx).
By the way, the line of code
Dim FName, SUBDIR, ROOTDIR As String
probably isn't doing what you think it is. It does NOT declare all these
variables as Strings. It declares FName and SUBDIR as Variants and only
ROOTDIR as a String. It is the same as
Dim FName As Variant, SUBDIR As Variant, ROOTDIR As String
However, this should not cause any problems.
Also, you should almost NEVER use the "End" statement. It is a poor
programming practice that can cause unexpected problems.
And on a final note, your computer's clock is wrong. Please fix it.
--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
"Jaemun" <x> wrote in message news:u4Xj7q5$(E-Mail Removed)...
> Hi again,
> My appologize. Sorry I forget to mentions that I'm using 2 types of code
> (Macro and Function).
>
> This macro code should bring up the popup Browser:-
> Sub OpenMyBrowser()
> Dim FName, SUBDIR, ROOTDIR As String
> FName = BrowseFolder("Testing")
> If FName = "" Then
> End
> Else
> End If
> 'procceed next code here
> End Sub
>
> I dont think there is a problem with "Sub OpenMyBrowser()".
> Probably someone could help me to modify the functions:-
>
> Private Const BIF_RETURNONLYFSDIRS As Long = &H1
> Private Const BIF_DONTGOBELOWDOMAIN As Long = &H2
> Private Const BIF_RETURNFSANCESTORS As Long = &H8
> Private Const BIF_BROWSEFORCOMPUTER As Long = &H1000
> Private Const BIF_BROWSEFORPRINTER As Long = &H2000
> Private Const BIF_BROWSEINCLUDEFILES As Long = &H4000
> Private Const MAX_PATH As Long = 260
>
> Type BrowseInfo
> hOwner As Long
> pidlRoot As Long
> pszDisplayName As String
> lpszINSTRUCTIONS As String
> ulFlags As Long
> lpfn As Long
> lParam As Long
> iImage As Long
> End Type
>
> Type SHFILEOPSTRUCT
> hwnd As Long
> wFunc As Long
> pFrom As String
> pTo As String
> fFlags As Integer
> fAnyOperationsAborted As Boolean
> hNameMappings As Long
> lpszProgressTitle As String
> End Type
>
> Declare Function SHGetPathFromIDListA Lib "shell32.dll" ( _
> ByVal pidl As Long, _
> ByVal pszBuffer As String) As Long
>
> Declare Function SHBrowseForFolderA Lib "shell32.dll" ( _
> lpBrowseInfo As BrowseInfo) As Long
>
>
> Function BrowseFolder(Optional Caption As String = "") As String
>
> Dim BrowseInfo As BrowseInfo
> Dim FolderName As String
> Dim ID As Long
> Dim Res As Long
>
> With BrowseInfo
> .hOwner = 0
> .pidlRoot = 0
> .pszDisplayName = String$(MAX_PATH, vbNullChar)
> .lpszINSTRUCTIONS = Caption
> .ulFlags = BIF_RETURNONLYFSDIRS
> .lpfn = 0
> End With
>
> FolderName = String$(MAX_PATH, vbNullChar)
>
> ID = SHBrowseForFolderA(BrowseInfo)
>
> If ID Then
> Res = SHGetPathFromIDListA(ID, FolderName)
> If Res Then
> BrowseFolder = Left$(FolderName, InStr(FolderName, _
> vbNullChar) - 1)
> End If
> End If
> End Function
>
> Thanks in advance.
> Jaemun.
>