一個系統有檔案匯出功能,點擊匯出按鈕后,可以在指定檔案夾中匯出PDF和Excel檔案,
匯出的時候有個程序,先在檔案夾中出現一個大小為0的檔案,然后那個檔案的體積會慢慢變大,
我想等檔案完全匯出完畢之后,放入outlook作為附件發出去,在實際操作中,出現了一些問題。
請老師幫我看看這倆問題如何解決,謝謝
1. 程序 ListFiles
剛剛點擊完匯出按鈕后,運行 ListFiles,有的時候 Str$ = Obj.Path 這一句會報錯,錯誤是 "File Not Found",
打開本地視窗發現,Obj 的所有屬性全都顯示為 "application-defined or object-defined error",
如果在點擊到處按鈕之后,等一段時間再去運行 ListFiles,就不會出現這個問題。
2. 程序 ReadFiles 和 AccessRight
利用這倆程序,來逐一判斷檔案夾中檔案是否處于被其他程式占用的狀態,如果檔案全都不被占用的話,就說明所有檔案全都匯出完成,
但在實際運行這倆程序的時候,會遇到一個問題,有的時候,那個系統本身就報錯了,錯誤是"檔案已經被其他應用程式打開",
估計是檔案剛剛生成的時候,就被這一句占用了 Open FilePath For Binary Lock Read Write As #1
Sub ListFiles()
Dim FSO As Object
Dim FSO_Folder As Object
Dim myPath$
Dim Obj
Dim Str$
Dim k1 As Long
myPath$ = "C:\Users\jim\Desktop\UIAutomation_VBA-master"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FSO_Folder = FSO.GetFolder(myPath)
For Each Obj In FSO_Folder.Files
Str$ = Obj.Path
Next Obj
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub ReadFiles()
Dim FSO As Object
Dim FSO_Folder As Object
Dim myPath$
Dim Obj
Dim Str$
Dim k1 As Long
myPath$ = "C:\Users\jim\Desktop\UIAutomation_VBA-master"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FSO_Folder = FSO.GetFolder(myPath)
Do
k1 = 0
For Each Obj In FSO_Folder.Files
k1 = k1 + AccessRight(Obj.Path)
Next Obj
DoEvents
Loop Until k1 = FSO_Folder.Files.Count
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function AccessRight(ByVal FilePath As String) As Long
On Error GoTo The_end
AccessRight = 0
Open FilePath For Binary Lock Read Write As #1
Close #1
AccessRight = 1
The_end:
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/40433.html
標籤:VB基礎類
