...
Sub ListFiles(fldObj 作为对象,掩码作为字符串)
调暗 fl 作为对象文件
Dim db As DAO.Database
设置 db = CurrentDb
Dim rst As DAO.Recordset
设置 rst = db.OpenRecordset("FileNames", dbOpenDynaset)
For Each fl In fldObj.files
For i = 1 To 16
Extn = Right(fl, i)
If InStr(Extn, ".") > 0 Then
Extn = Right(Extn, i - 1)
i = 16
End If
Next i
On Error GoTo ThisError
If InStr(fl, "$") > 0 _
Or Extn = "lnk" _
Or Extn = "ppm" _
Or InStr(fl, ".") = 0 _
Or Len(Extn) > 4 _
Or Extn = "dat" _
Or CLng(fl.Size / 1024) < 64 _
Or Extn = "sst" _
Or Extn = "ldb" _
Or Extn = "log" _
Or Extn = "WMF" _
Or Extn = "json" Then
SetAttr fl, vbNormal
Kill fl
GoTo LoopOut
End If
ThisError:
If fl.Name Like Mask Then
rst.AddNew
rst("File").Value = Left(fl.Name, Len(fl.Name) - (Len(Extn) + 1))
If (CStr(Err.Number)) = 0 Then rst("ErrNum&Description").Value = " " Else
rst("ErrNum&Description").Value = CStr((Err.Number) & " " & Err.Description)
rst("KB Size").Value = CLng(fl.Size / 1024)
'https://stackoverflow.com/questions/69432605/get-file-size-filelen-of-a-file-that'
'-has-unicode-characters-in-the-name-exce'
rst("Ext").Value = Extn
rst("Home").Value = fldObj
rst.Update
End If
Err.Clear
LoopOut:
Next
End Sub
...
我正在运行的代码源自“https://stackoverflow.com/questions/14245712/cycle-through-sub-folders-and-files-in-a-user-specified-root-directory”,一切都运行得很好接受“On Error GoTo ThisError”行无效,创建的错误不会传递到“ThisError:”行。我已将 Tools\Options\General 设置为“Break on Unhandled Errors”,并在“Break in Class Module”上尝试过,这也没有影响,并且错误陷阱仍然不起作用。 我的想法是,这可能是类模块中引发错误的结果。如果是这种情况,那么我不知道如何解决这个问题,也不知道这样做的后果。我怀疑背后的逻辑来自于阅读““在未处理的错误上中断”在大多数情况下都有效,但在调试类模块时会出现问题。在开发过程中,如果错误捕获设置为“在未处理的错误上中断”并且类模块中发生错误,调试器停止在调用类的行上,而不是类中的有问题的行上,这使得查找和修复问题变得非常痛苦,我们建议使用“中断类模块”,它会停止在实际崩溃的行上。 @ 'https://www.fmsinc.com/free/newtips/VBA/BasicErrorHandling.asp#:~:text=Verify%20Error%20Trapping%20Setting&text=From%20the%20module%20editor%20(IDE,%22On% 20错误%20恢复%20下一步%22.