Greed Island

纪念我喜欢的动画片Hunter X Hunter。

星期三, 六月 03, 2009

vba的文件操作

Posted by 박용진

明天或许有用,先贴上。

Sub Macro()
Dim FSO, fold, i As Integer
Set FSO = CreateObject("Scripting.FileSystemObject")
Set fold = FSO.GetFolder("D:\study")
i = 1
For Each myFile In fold.SubFolders
If StrConv(Right(myFile.Name, 4), vbLowerCase) = ".pdf" Then
'Workbooks.Open Filename:=myFile.Path
Sheets(1).Cells(i, 1) = myFile.Path
i = i + 1
End If
Next
Set FSO = Nothing
End Sub
Sub Test()
Dim FoundFile As String, i As Integer
i = 1
'FoundFile = Dir(ThisWorkbook.Path & "\*.xls")
FoundFile = Dir("D:\study\C++\*.pdf")
Do While FoundFile <> ""
If FoundFile = "." Then
Debug.Print FoundFile
End If
Sheets(1).Cells(i, 1) = FoundFile
FoundFile = Dir()
i = i + 1
Loop
End Sub

Sub Test1()
EnumFiles "d:\study\c++" ' 遍历指定目录下的文件
End Sub

Sub EnumFiles(ByVal sPath As String)
Dim fs As New FileSystemObject
GetFile fs.GetFolder(sPath)
Set fs = Nothing
End Sub

Sub GetFile(ByVal fldParent As Folder)
Dim fldSub As Folder, fSub As File
For Each fldSub In fldParent.SubFolders
GetFile fldSub
Next
For Each fSub In fldParent.Files
If UCase(Right(Trim(fSub), 3)) = "PDF" Then ' 文件类型判断
Debug.Print fSub
End If
Next
Set fldSub = Nothing
Set fSub = Nothing
End Sub

0 comments: