Make a class module called fileDialog
Option Explicit
Private Declare Function GetOpenFileName Lib _
"comdlg32.dll" _
Alias "GetOpenFileNameA" (lpOpenFilename As
OPENFILENAME) _
As Long
Private Declare Function GetSaveFileName Lib _
"comdlg32.dll" _
Alias "GetSaveFileNameA" (lpOpenFilename As
OPENFILENAME) _
As Long
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 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
Const BIF_RETURNONLYFSDIRS = &H1
Const BIF_DONTGOBELOWDOMAIN = &H2
Const BIF_STATUSTEXT = &H4
Const BIF_RETURNFSANCESTORS = &H8
Const BIF_BROWSEFORCOMPUTER = &H1000
Const BIF_BROWSEFORPRINTER = &H2000
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter 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
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Function fdlgGetOpenFileName(strFilter As String)
Dim rc As Long
Dim pOpenFileName As OPENFILENAME
Const MAX_BUFFER_LENGTH = 256
With pOpenFileName
.hwndOwner = Application.hWndAccessApp
.lpstrTitle = "Open"
.lpstrInitialDir = CurrentProject.Path
.lpstrFilter = Replace(strFilter, ";", Chr(0))
.nFilterIndex = 1
.lpstrFile = String(MAX_BUFFER_LENGTH, 0)
.nMaxFile = MAX_BUFFER_LENGTH - 1
.lpstrFileTitle = .lpstrFile
.nMaxFileTitle = MAX_BUFFER_LENGTH - 1
.lStructSize = Len(pOpenFileName)
End With
rc = GetOpenFileName(pOpenFileName)
If rc Then
fdlgGetOpenFileName = Left(pOpenFileName.lpstrFile,
pOpenFileName.nMaxFile)
Else
fdlgGetOpenFileName = ""
End If
End Function
Function fdlgGetSaveFileName(strFilter As String, strPath
As String, Optional strDefExt As String)
Dim rc As Long
Dim pOpenFileName As OPENFILENAME
Const MAX_BUFFER_LENGTH = 256
With pOpenFileName
.hwndOwner = Application.hWndAccessApp
.lpstrTitle = "Save"
.lpstrInitialDir = strPath 'CurrentProject.Path
.lpstrFilter = Replace(strFilter, ";", Chr(0))
.nFilterIndex = 1
.lpstrDefExt = strDefExt
.lpstrFile = String(MAX_BUFFER_LENGTH, 0)
.nMaxFile = MAX_BUFFER_LENGTH - 1
.lpstrFileTitle = .lpstrFile
.nMaxFileTitle = MAX_BUFFER_LENGTH - 1
.lStructSize = Len(pOpenFileName)
End With
rc = GetSaveFileName(pOpenFileName)
If rc Then
fdlgGetSaveFileName = Left(pOpenFileName.lpstrFile,
pOpenFileName.nMaxFile)
Else
fdlgGetSaveFileName = pOpenFileName.lpstrFile '""
End If
End Function
Function fdlgGetFolder(Optional Title As String, _
Optional Hwnd) As String
Dim bi As BROWSEINFO
Dim PIDL As Long
Dim Folder As String
Folder = String(255, Chr$(0))
With bi
If IsNumeric(Hwnd) Then .hOwner = Hwnd
.ulFlags =
BIF_DONTGOBELOWDOMAIN 'BIF_RETURNFSANCESTORS '&
BIF_DONTGOBELOWDOMAIN
.pidlRoot = 0
If Title <> "" Then
.lpszTitle = Title & Chr(0)
Else
.lpszTitle = "Select a Folder"
End If
End With
PIDL = SHBrowseForFolder(bi)
If SHGetPathFromIDList(ByVal PIDL, ByVal Folder) Then
fdlgGetFolder = Folder
Else
fdlgGetFolder = "" 'Folder
End If
End Function