- Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
- (pOpenfilename As OPENFILENAME) As Long
- Public Const OFN_PATHMUSTEXIST = &H800
- Public Const OFN_FILEMUSTEXIST = &H1000
- Public Const OFN_HIDEREADONLY = &H4 '隐蔽只读复选框
- Public Const OFN_ALLOWMULTISELECT = &H200
- Private Const OFN_EXPLORER As Long = &H80000
- Public 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
- '选择文件函数GetFile
- Function GetFile(strTitle As String, strFilter As String, Optional MultiSelect As Boolean = False, Optional strIniDir As String) As String
- On Error Resume Next
- Dim FileName As String
- Dim OFileBox As OPENFILENAME
- With OFileBox
- .lpstrTitle = strTitle '对话框标题
- .lpstrInitialDir = strIniDir '初始目录
- .lStructSize = Len(OFileBox)
- .hwndOwner = ThisDrawing.HWND
- If MultiSelect Then
- .flags = OFN_EXPLORER Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY Or OFN_ALLOWMULTISELECT
- Else
- .flags = OFN_EXPLORER Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY
- End If
- .lpstrFile = String$(255, 0)
- .nMaxFile = 255
- .lpstrFileTitle = String$(255, 0)
- .nMaxFileTitle = 255
- .lpstrFilter = strFilter '过滤器
- .nFilterIndex = 1
- End With
- lntFile = GetOpenFileName(OFileBox) '执行打开对话框
- If lntFile <> 0 Then
- FileName = Left(OFileBox.lpstrFile, InStr(OFileBox.nFileOffset + 1, OFileBox.lpstrFile, _
- Chr(0) & Chr(0), vbBinaryCompare))
- GetFile = FileName
- Else
- GetFile = ""
- End If
- End Function
- '示例
- Sub gf()
- Dim f As String
- f = GetFile("明经通道 http://www.mjtd.com VBA 示例", "图形文件(*.dwg)" & vbNullChar & "*.dwg", True)
- Debug.Print f
- End Sub
|