|
Sub 第三代遍历文件夹()
Dim pPath$, f As Object, fd As Object, fso As Object, Stack$(), top&, n&, stxt$, doc As Document, x&
pPath = "C:\Users\Administrator\Desktop\123\20-888"
Set fso = CreateObject("Scripting.FileSystemObject")
top = 1
ReDim Stack(0 To top)
Do While top >= 1
For Each f In fso.getfolder(pPath).Files
n = n + 1
stxt = f.Path
If stxt Like "*材料*.doc*" Then
Set doc = Documents.Open(FileName:=stxt)
x = x + 1
End If
Next
For Each fd In fso.getfolder(pPath).SubFolders
Stack(top) = fd.Path
top = top + 1
If top > UBound(Stack) Then ReDim Preserve Stack(0 To top)
Next
If top > 0 Then pPath = Stack(top - 1): top = top - 1
Loop
Set f = Nothing
Set fd = Nothing
Set fso = Nothing
MsgBox "文件夹包含 " & n & " 个文件!" & vbCr & "共处理 Word 文档(*.docx/*.doc) " & x & " 个!", 0 + 48
End Sub
如上:是一个文件搜索的代码。
我要搜索含有“材料”的文件名的文档并打开。
现在有一个需求,档搜索第一份含有“材料”文件名的文档时,即跳出此循环,不再继续寻找第二份含有“材料”名称的文档。
代码要如何更改?
望大神指点!!!!!!!!!!
|
|