?
Solved

Browse for Folder using UNC path names

Posted on 2004-04-02
5
Medium Priority
?
3,094 Views
Last Modified: 2008-12-12

I use a function called "BrowseForFolder" that I copied from a web source (can't remember where - sorry), that uses windows API calls (SHBrowseForFolder and others) to enable me to browse for a particular folder.  This function uses a "StartDirectory" arguement to allow me to define the initial browse directory.  This works fine, until I set my start directory to be a UNC file path, such as \\servername\sharename.  The function call still works (no errors), but it seems the API calls can't recognise this form of "StartDdirectory" parameter, so the browse starts at "C:\"  ...

How can I modify this code to allow for UNC path support?  The full code as originally supplied to me ...


##########


Option Explicit

'--------------Start "browse for folder" declarations------------
Private Const BIF_STATUSTEXT = &H4&
Private Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_DONTGOBELOWDOMAIN = 2
Private Const MAX_PATH = 260

Private Const WM_USER = &H400
Private Const BFFM_INITIALIZED = 1
Private Const BFFM_SELCHANGED = 2
Private Const BFFM_SETSTATUSTEXT = (WM_USER + 100)
Private Const BFFM_SETSELECTION = (WM_USER + 102)

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
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

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 m_CurrentDirectory As String   'The current directory
'-----End "Browse for folder" declarations----


'-------------Handle "browser for folder dialog----------
Public Function BrowseForFolder(title As String, startDir As String, Optional ByVal hwnd As Long = 0) As String
  'Opens a Treeview control that displays the directories in a computer

  Dim lpIDList As Long
  Dim szTitle As String
  Dim sBuffer As String
  Dim tBrowseInfo As BrowseInfo
  m_CurrentDirectory = startDir & vbNullChar

  szTitle = title
  With tBrowseInfo
    .hWndOwner = hwnd
    .lpszTitle = lstrcat(szTitle, "")
    .ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN + BIF_STATUSTEXT
    .lpfnCallback = GetAddressofFunction(AddressOf BrowseCallbackProc)  'get address of function.
  End With

  lpIDList = SHBrowseForFolder(tBrowseInfo)
  If (lpIDList) Then
    sBuffer = Space(MAX_PATH)
    SHGetPathFromIDList lpIDList, sBuffer
    sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
    BrowseForFolder = sBuffer
  Else
    BrowseForFolder = ""
  End If
 
End Function


Private Function BrowseCallbackProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal lp As Long, ByVal pData As Long) As Long
 
  Dim lpIDList As Long
  Dim ret As Long
  Dim sBuffer As String
 
  On Error Resume Next  'Sugested by MS to prevent an error from
                        'propagating back into the calling process.
     
  Select Case uMsg
 
    Case BFFM_INITIALIZED
      Call SendMessage(hwnd, BFFM_SETSELECTION, 1, m_CurrentDirectory)
     
    Case BFFM_SELCHANGED
      sBuffer = Space(MAX_PATH)
     
      ret = SHGetPathFromIDList(lp, sBuffer)
      If ret = 1 Then
        Call SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, sBuffer)
      End If
     
  End Select
 
  BrowseCallbackProc = 0
 
End Function

' This function allows you to assign a function pointer to a vaiable.
Private Function GetAddressofFunction(add As Long) As Long
  GetAddressofFunction = add
End Function
'-------------End handle "browser for folder" dialog----------


Sub browseTest()
    Dim myPath As String
    myPath = BrowseForFolder("title", "c:\")

    If myPath = "" Then
        MsgBox "User pressed Cancel"
    Else
        MsgBox "Folder selected was: " + myPath
    End If
End Sub


##########
0
Comment
Question by:JimbobW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 10740627
Messy but if nothing else than
If you want you can map the network drive and then just pass a letter

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20860580.html

I:\ or something
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 10740641
are you sure you added the "\\"

and

temp = "\\" & Trim(NetWOrkPath)

You may be off by a space or so
0
 
LVL 48

Accepted Solution

by:
Mikal613 earned 1360 total points
ID: 10740655
0
 

Author Comment

by:JimbobW
ID: 10741222

You're not wrong there!  The Browse Dialogue Server/VB6 from CCRP that you linked me to does just the job.  UNC names work perfectly!

Points to you!
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 10741259
thanks JimBob
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question