本文系原创文章,转载请注明出处(jungleosng.yculblog.com)
1.打开标准文件目录对话框.
首先需要引入MicroSoft Common Dialog 6.0,然后把工具栏上多出的图标放置到画
面上,程序中这样写:
'// 显示标准文件对话框
With cdlg
.FileName = ""
.Filter = "Excel File(*.xls)|*.xls" '// 文件描述及类型
.InitDir = App.Path + "\"
.ShowSave '// 显示保存状态,如果是打开
状态则为ShowOpen
If Len(.FileName) > 0 Then '// 如果文件名有效
Screen.MousePointer = 11 '// 鼠标变成等待状态
Call StartMergeFile(.FileName, strArrAllFile)'// 对文件进行
处理
Screen.MousePointer = 1 '// 鼠标变成正常状态
End If
End With
运用上述代码一般在一个按钮的点击事件中.
2.判断文件/目录是否存在
Public Function isFileExist(strFile As String) As Boolean
isFileExist=(Dir(strFile, vbDirectory) <> "")
End Function
这个函数主要使用了Dir函数.
3.以缺省方式打开文件
首先我们需要引入Api函数ShellExecute
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
然后在代码中这样调用:
Dim lngTmp As Long
lngTmp = ShellExecute(Me.hwnd, "open", strFileName, "", "", 1)
strFileName就是你要打开的文件名,ShellExecute会通知Windows找到strFileName
这个文件的缺省打开方式,再以这个方式打开.大家可以试一试效果.
4.遍历某目录下某类型文件(不遍历子目录)
函数如下:
Public Function getProperFileInDir(strDir As String, _
strFilter As String, _
strArr() As String)
Dim strFileName As String
'// get first file
strFileName = Dir(strDir + "\" + strFilter, vbNormal Or vbArchive Or vbHidden)
Dim intFileCount As Integer
Do While Len(strFileName) > 0
ReDim Preserve strArr(0 To intFileCount)
strArr(intFileCount) = strDir + "\" + strFileName
intFileCount = intFileCount + 1
strFileName = Dir
Loop
End Function
调用方式如下:
Dim strArrAllFile() As String
ReDim Preserve strArrAllFile(0 To 0)
Call getProperFileInDir(txtPath, txtType, strArrAllFile)
