我有以下模块检查文件数,并在缺少文件时写入.txt文件:
Sub CheckFiles(strDir As String, strType As String, chknum As Integer)
Dim file As Variant, i As Integer
Dim sFilename As String
strDir = ThisWorkbook.Path & "\Source\"
sFilename = ThisWorkbook.Path & "\Logs.txt"
If Right(strDir, 1) <> "\" Then strDir = strDir & "\"
file = Dir(strDir & strType)
While (file <> "")
i = i + 1
file = Dir
Wend
If i <> chknum Then
' Archive file at certain size
If FileLen(sFilename) > 20000 Then
FileCopy sFilename _
, Replace(sFilename, ".txt", Format(Now, "ddmmyyyy hhmmss.txt"))
Kill sFilename
End If
' Open the file to write
Dim filenumber As Variant
filenumber = FreeFile
Open sFilename For Append As #filenumber
Print #filenumber, CStr(Now) & ", " & "Source file: " & strType & " is missing " & chknm - i & " file(s)"
Close #filenumber
End If
End Sub
从此模块调用CheckFiles子项:
Sub Report()
'other code
Call CheckFiles("", "File1.xlsx", 1)
Call CheckFiles("", "File2.xlsx", 1)
Call CheckFiles("", "File3.xlsx", 1)
Call CheckFiles("", "File4*.xlsx", 24)
'other code
End Sub
如果文件丢失,我要退出Report
子项。
当尝试检测到错误但不起作用时,我尝试在Error Goto EH
的开头添加On CheckFiles
,并在Report
子末添加以下内容。< / p>
Done:
Exit Sub
EH:
Call EmailError
End Sub
我该如何实现?