Opening Specific Directory

BWG3

Registered User.
Local time
, 22:38
Joined
Sep 22, 2009
Messages
44
I am new to VB programming. I found this code that opens up a directory window, but instead of it just opening up the directory window, I would like to be able to select a specfic server path, such as "My network places\Server123\folder". I'm just not sure what part of the code I need to change or modify in order to have the ability to select the path I want to open when a browse button is clicked (this is to eliminate having the click 5 times to find a folder since the folder path is going to be the same every time the browse button is clicked). The code is listed below.

Option Explicit
Option Compare Database

'API Functions
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Boolean
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias _
"GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Boolean
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
'constants
Private Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_DONTGOBELOWDOMAIN = 2
Private Const MAX_PATH = 260
Private Const ALLFILES = "All Files"
Private Const OFN_ALLOWMULTISELECT = &H200
Private Const OFN_CREATEPROMPT = &H2000
Private Const OFN_EXPLORER = &H80000
Private Const OFN_FILEMUSTEXIST = &H1000
Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_NOCHANGEDIR = &H8
Private Const OFN_NODEREFERENCELINKS = &H100000
Private Const OFN_NONETWORKBUTTON = &H20000
Private Const OFN_NOREADONLYRETURN = &H8000
Private Const OFN_NOVALIDATE = &H100
Private Const OFN_OVERWRITEPROMPT = &H2
Private Const OFN_PATHMUSTEXIST = &H800
Private Const OFN_READONLY = &H1
Private Const OFN_SHOWHELP = &H10
'types
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 Type MSA_OPENFILENAME
' Filter string used for the Open dialog filters.
' Use MSA_CreateFilterString() to create this.
' Default = All Files, *.*
strFilter As String
' Initial Filter to display.
' Default = 1.
lngFilterIndex As Long
' Initial directory for the dialog to open in.
' Default = Current working directory.
strInitialDir As String
' Initial file name to populate the dialog with.
' Default = "".
strInitialFile As String
strDialogTitle As String
' Default extension to append to file if user didn't specify one.
' Default = System Values (Open File, Save File).
strDefaultExtension As String
' Flags (see constant list) to be used.
' Default = no flags.
lngFlags As Long
' Full path of file picked. When the File Open dialog box is
' presented, if the user picks a nonexistent file,
' only the text in the "File Name" box is returned.
strFullPathReturned As String
' File name of file picked.
strFileNameReturned As String
' Offset in full path (strFullPathReturned) where the file name
' (strFileNameReturned) begins.
intFileOffset As Integer
' Offset in full path (strFullPathReturned) where the file extension begins.
intFileExtension As Integer
End Type
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As Long
nMaxCustrFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustrData As Long
lpfnHook As Long
lpTemplateName As Long
End Type
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Function GetDirectory
' Inputs: szTitle - Text Prompt in Dialog Box
' CallingForm - Form that is to act as owner of dialog (usually Me)
' Outputs:Returns Selected Directory Path
' Comments: Opens a Treeview control that displays the directories in a computer
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Function GetDirectory(szTitle As String, CallingForm As Form) As String
Dim lpIDList As Long
Dim sBuffer As String
Dim tBrowseInfo As BrowseInfo
With tBrowseInfo
.hwndOwner = CallingForm.Hwnd
.lpszTitle = lstrcat(szTitle, "")
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN
End With
lpIDList = SHBrowseForFolder(tBrowseInfo)

If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
GetDirectory = sBuffer

End If
End Function

Thank you in advance.
Brandon
 
Hi,

I am new to VB too but I used these tutorials:http://www.vbtutor.net/vb2008/vb2008tutor.html

Lesson 21 is about opening and writing to a text file - I am guessing the correct code to open a window and browse is in there. It allows you to open a dialog box like when you save a word document.

Hope it helps...
 
Mcclunyboy, thank you, this will help me learn more. I have the selected folder directory path storing in a text box using the above code, but all I really want to know is if there is any way I can change or modify the above code so I can select the directory path instead of it just opening the generic directory box with all the drives and servers listed.
 
Glancing over the code, this is the area you are looking for:

Code:
' Initial directory for the dialog to open in.
' Default = Current working directory.
strInitialDir As String
 
........
 
lpstrInitialDir As String

so I would guess that in the function, you can set lpstrInitialDir to what you want. You should even be able to include it in the Arguement for the function.

I dont use code like this, so take the above as more of a guess that fact. Maybe someone else can confirm or deny if what i'm suggesting is true or not. Just remember...trial and error can be your friend :)
 
Thanks you, Scooterbug. Yes, I have been using the trial and error process, but have not had any luck so far. I will try your suggestion and hopefully someone can confirm or give me another hint.
 
I have cleaned up the code so it is easier to understand when I post it. I can still not locate where it is defining the path to open the directory dialogue box. Can anyone please help me find out what part of the code is generating the directory dialogue box so I can modify it to open to a specific server path.

Option Compare Database
Option Explicit

Private Type BrowseInfo
hwndOwner As Long
pidlRoot As Long
sDisplayName As String
sTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Private Declare Function SHBrowseForFolder Lib "shell32.dll" (bBrowse As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" (ByVal lItem As Long, ByVal sDir As String) As Long

' Let the user browse for a directory. Return the
' selected directory. Return an empty string if
' the user cancels.
Public Function BrowseForDirectory() As String
Dim browse_info As BrowseInfo
Dim item As Long
Dim dir_name As String
'modified for MS Access/VBA
With browse_info
' .hwndOwner = Application.hWndAccessApp
.pidlRoot = 0
.sDisplayName = Space$(260)
.sTitle = "Select Directory"
.ulFlags = 1 ' Return directory name.
.lpfn = 0
.lParam = 0
.iImage = 0
End With
item = SHBrowseForFolder(browse_info)

If item Then
dir_name = Space$(260)
If SHGetPathFromIDList(item, dir_name) Then
BrowseForDirectory = Left(dir_name, _
InStr(dir_name, Chr$(0)) - 1)
Else
BrowseForDirectory = ""
End If
End If
End Function
 
Ross, thank you so much! It's exactly what I needed.
 

Users who are viewing this thread

Back
Top Bottom