標題:
[發問]
Application.FileSearch 無法使用
[打印本頁]
作者:
h60327
時間:
2014-11-5 20:45
標題:
Application.FileSearch 無法使用
各位先進此統計表係以office 2003版製作,但因公司更新為office 2010版,其中原有部分功能無法使用
目前知道係因Application.FileSearch無法再使用,經上網查詢可使用dir代替,但自己功力不足仍無法正常執行,
還請各位先進多加指導,很抱歉這是一個word檔但相信程式碼是相通的,若有發錯請見諒
作者:
bobomi
時間:
2014-11-5 20:58
http://forum.twbts.com/thread-671-1-1.html
http://forum.twbts.com/viewthrea ... ighlight=FileSearch
作者:
h60327
時間:
2014-11-6 08:42
這二則我也都有搜尋到,但是自己卻套用不來,所以才再請教解決方式,不勝感激
作者:
stillfish00
時間:
2014-11-6 16:08
回復
3#
h60327
寫了一個物件類別模組,只實現你有用到的部分FileSearch功能
使用方法:
1. Project 中插入一個物件類別模組,物件類別模組Name改為clsFileSearch,貼上以下程式碼
Private msLookIn As String
Private msFileName As String
Private mbSearchSubFolders As Boolean
Private mcolFoundFiles As Collection
Private Sub Class_Initialize()
Set mcolFoundFiles = New Collection
End Sub
Public Property Get LookIn() As String
LookIn = msLookIn
End Property
Public Property Let LookIn(ByVal sLookIn As String)
msLookIn = sLookIn
End Property
Public Property Get FileName() As String
FileName = msFileName
End Property
Public Property Let FileName(ByVal sFileName As String)
msFileName = sFileName
End Property
Public Property Get SearchSubFolders() As Boolean
SearchSubFolders = mbSearchSubFolders
End Property
Public Property Let SearchSubFolders(ByVal bSearchSubFolders As Boolean)
mbSearchSubFolders = bSearchSubFolders
End Property
Public Property Get FoundFiles() As Collection
Set FoundFiles = mcolFoundFiles
End Property
Public Function Execute() As Long
Dim oFSO As Object, oFolder As Object
Dim oFile
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set mcolFoundFiles = New Collection '清空
If oFSO.FolderExists(msLookIn) Then
Set oFolder = oFSO.GetFolder(msLookIn)
For Each oFile In oFolder.Files
If oFile.Name Like msFileName Then mcolFoundFiles.Add oFile.Path
Next
If mbSearchSubFolders Then FindSubFolder oFolder.subfolders
End If
Execute = mcolFoundFiles.Count
End Function
Private Sub FindSubFolder(ByRef oSub As Object)
Dim x, oFile
For Each x In oSub
For Each oFile In x.Files
If oFile.Name Like msFileName Then mcolFoundFiles.Add oFile.Path
Next
FindSubFolder x.subfolders
Next
End Sub
複製代碼
2. 然後把你的CommandButton1_Click()中的
Set fs1 = Application.FileSearch
改成
Set fs1 = New clsFileSearch
作者:
h60327
時間:
2014-11-6 20:27
回復
4#
stillfish00
感謝stillfish00前輩的幫忙,終於可以使用,減少很多的統計時間 。
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)