我有一個打開word檔案的回圈,但是有一些已損壞,我也無法手動打開它們,
我用來打開的代碼是,
Set newdoc = objWord.Documents.Open(saveFolder & Filename)
這失敗并出現錯誤
“運行時錯誤 5792,檔案似乎已損壞”。
在嘗試打開檔案之前是否可以判斷檔案已損壞?
uj5u.com熱心網友回復:
不,但您可以在嘗試打開它時對其進行測驗。
On Error Resume Next ' deactivate error reporting
Set newdoc = Nothing
Set newdoc = objWord.Documents.Open(saveFolder & Filename)
' check if trying to open it produced an error
If Err.Number = 5792 Then
Msgbox "'" & saveFolder & Filename & "' is corrupt."
Err.Clear
End If
On Error Goto 0 ' re-activate error reporting. Do not forget this!
' if an error occured during opening then newdoc is nothing
If Not newdoc Is Nothing
' do what you want to do with the opened file
Else
' do what you want to do with a corrupt file
End If
uj5u.com熱心網友回復:
您最好的選擇是使用錯誤處理。
就像是
On Error GoTo skip
Set newdoc = objWord.Documents.Open(saveFolder & Filename)
On Error GoTo 0
Exit Function
skip:
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/439839.html
標籤:vba
