2006年5月10日
'例: CommonFileOpenSave(, , "所有文件(*.*)" & Chr(0) & "*.*" & Chr(0))
Option Compare Database
Option Explicit
Type tagOPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
strFilter As String
strCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
strFile As String
nMaxFile As Long
strFileTitle As String
nMaxFileTitle As Long
strInitialDir As String
strTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
strDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Declare Function apiGetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (ofn As tagOPENFILENAME) As Boolean
Private Declare Function apiGetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (ofn As tagOPENFILENAME) As Boolean
Public Function CommonFileOpenSave( _
Optional ByRef flags As Variant, _
Optional ByVal InitialDir As Variant, _
Optional ByVal Filter As Variant, _
Optional ByVal FilterIndex As Variant, _
Optional ByVal DefaultExt As Variant, _
Optional ByVal Filename As Variant, _
Optional ByVal DialogTitle As Variant, _
Optional ByVal hwnd As Variant, _
Optional ByVal OpenFile As Variant) As Variant
Dim ofn As tagOPENFILENAME
Dim strFilename As String
Dim strFileTitle As String
Dim fResult As Boolean
Dim strErrNotes As String
On Error GoTo CommonFileOpenSave_ErrorIf IsMissing(InitialDir) Then InitialDir = CurDir
If IsMissing(Filter) Then Filter = ""
If IsMissing(FilterIndex) Then FilterIndex = 1
If IsMissing(flags) Then flags = 0&
If IsMissing(DefaultExt) Then DefaultExt = ""
If IsMissing(Filename) Then Filename = ""
If IsMissing(DialogTitle) Then DialogTitle = ""
If IsMissing(hwnd) Then hwnd = Application.hWndAccessApp
If IsMissing(OpenFile) Then OpenFile = TruestrFilename = Left(Filename & String$(255, 0), 255)
strFileTitle = String$(255, 0)With ofn
.lStructSize = Len(ofn)
.hwndOwner = hwnd
.strFilter = Filter
.nFilterIndex = FilterIndex
.strFile = strFilename
.nMaxFile = Len(strFilename)
.strFileTitle = strFileTitle
.nMaxFileTitle = Len(strFileTitle)
.strTitle = DialogTitle
.flags = flags
.strDefExt = DefaultExt
.strInitialDir = InitialDir
.hInstance = 0
.strCustomFilter = String(255, 0)
.nMaxCustFilter = 255
.lpfnHook = 0
End With
If OpenFile Then
fResult = apiGetOpenFileName(ofn)
Else
fResult = apiGetSaveFileName(ofn)
End If
If fResult Then
If Not IsMissing(flags) Then flags = ofn.flags
CommonFileOpenSave = TrimNull(ofn.strFile)
Else
CommonFileOpenSave = Null
End IfCommonFileOpenSave_WrapUp:
Exit FunctionCommonFileOpenSave_Error:
CommonFileOpenSave = Null
GoTo CommonFileOpenSave_WrapUpEnd Function
Public Function AddFilterItem(strFilter As String, _
strDescription As String, Optional varItem As Variant) As StringDim strErrNotes As String
If IsMissing(varItem) Then varItem = "*.*"
AddFilterItem = strFilter & _
strDescription & vbNullChar & _
varItem & vbNullCharEnd Function
Private Function TrimNull(ByVal strItem As String) As String
Dim intPos As Integer
Dim strErrNotes As StringintPos = InStr(strItem, vbNullChar)
If intPos > 0 Then
TrimNull = Left(strItem, intPos - 1)
Else
TrimNull = strItem
End IfEnd Function
选择目录的代码Option ExplicitPrivate Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_DONTGOBELOWDOMAIN = 2
Private Const MAX_PATH = 260Private 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 TypePrivate 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 LongPublic Function OpenDirectoryTV(Optional odtvTitle As String) As String
Dim lpIDList As Long
Dim sBuffer As String
Dim szTitle As String
Dim tBrowseInfo As BrowseInfo
szTitle = odtvTitle
lpIDList = SHBrowseForFolder(tBrowseInfo)
If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
OpenDirectoryTV = sBuffer
End If
End FunctionPrivate Sub Command0_Click()
Me.Text1 = OpenDirectoryTV("请选择一个目录")
End Sub

0 回复,0 引用: 标准对话框代码(打开,另存为,选择目录)
添加回复