我有一些我在網上找到的代碼,它的效果很好。它列出了指定文件夾中的任何文件及其屬性。我只需要修改這個返回文件列表的函數。我需要它在所有文件的給定路徑中查找。所以如果它是一個文件夾,打開它並返回這些文件。我最終需要得到這個標記每個文件(所以在Excel中的一列),它來自哪裏。如果有可能以某種方式創建一個水印,那就太好了......但是現在,只要打開一個文件夾,如果它找到並列出這些文件就太棒了!列表文件的作品,但我如何列出文件夾中的文件(VBA,Excel)?
任何幫助真的很感激!
Private Function fcnGetFileList(ByVal strPath As String, Optional strFilter As String) As Variant
' Returns a one dimensional array with filenames
' Otherwise returns False
Dim f As String
Dim i As Integer
Dim x As Integer
Dim FileList() As String
If strFilter = "" Then strFilter = "*.*" 'can provide an extension to check?
'set the path
Select Case Right$(strPath, 1)
Case "\", "/"
strPath = Left$(strPath, Len(strPath) - 1)
End Select
ReDim Preserve FileList(0)
f = Dir$(strPath & "\" & strFilter)
Do While Len(f) > 0
ReDim Preserve FileList(i) As String
FileList(i) = f
i = i + 1
f = Dir$()
Loop
If FileList(0) <> Empty Then
fcnGetFileList = FileList
Else
fcnGetFileList = False
End If
End Function
你多久看一次自己這樣做? – Fionnuala 2010-01-18 17:14:45
你真的爲你的所有功能使用「fcn」前綴嗎?這是匈牙利記法太過分了! :-) [然而你有一個名爲'f'的字符串變量...] – 2010-01-19 11:35:22
@Remou - 代碼將每天/每週運行一系列報告來查找文檔屬性等。 @Gary - haha,這不是我的代碼 - 它是我下載的一部分! – 2010-01-20 11:07:24