我有一個捲起文件,可打開多個Excel工作簿並將數據從這些文件複製到主文件中。該計劃已運行好幾個月,但在過去幾天它打開一些文件時失敗。我收到以下錯誤消息。VBA文件無法打開
運行時錯誤「1004」:
Excel無法打開文件「filename.xlsm」,因爲文件格式或文件擴展名無效。驗證該文件是否已損壞,並且文件擴展名與文件的格式相匹配尚未損壞,並且文件擴展名與文件的格式相匹配。
如果我點擊調試並繼續運行該程序,然後打開文件沒有問題。如果我重新啓動程序,它仍然無法打開文件,但它永遠不會是相同的文件。當我進入它們並且文件擴展名是正確的時,我無法找到工作簿出現問題的任何問題。我有錯誤處理,以檢查是否有任何人目前在工作簿中,所以我不認爲這是可能的。
任何幫助,將不勝感激, 謝謝。
If Not FileLocked(CStr(FoundFiles(iIndex))) Then
On Error GoTo contentErr
Workbooks.Open FoundFiles(iIndex) ', UpdateLinks:=xlUpdateLinksNever
On Error GoTo 0
Application.Run ("'Auto Update Roll-Up.xlsm'!Update")
With Workbooks(tempvar(iIndex - 1))
.Close False
LogInformation ("Completed " & tempvar(iIndex - 1) & " at " & Now)
'Application.EnableEvents = False
'.Close True
'Application.EnableEvents = True
End With
End If
Continue:
Next iIndex
On Error Resume Next
DisplayAlerts = False
Workbooks("Brickman Roll-Up Template.xlsm").Close savechanges:=True
'Workbooks("Brickman Roll-Up Template Test.xlsm").Close savechanges:=True
SetAttr rollupPath, vbReadOnly
Workbooks("Auto Update Roll-Up.xlsm").Close savechanges:=False
DisplayAlerts = True
LogInformation ("Program ended at " & Now)
Application.Quit
contentErr:
If Err.Number = 1004 Then
LogInformation ("_______There is unreadable content in " & Chr(34) & tempvar(iIndex - 1) & Chr(34) & "_______")
GoTo Continue
End If
End Sub
Function FileLocked(strFileName As String) As Boolean
On Error Resume Next
' If the file is already opened by another process,
' and the specified type of access is not allowed,
' the Open operation fails and an error occurs.
Open strFileName For Binary Access Read Write Lock Read Write As #1
Close #1
' If an error occurs, the document is currently open.
If Err.Number <> 0 Then
' Display the error number and description.
LogInformation ("Couldn't open " & strFileName & " because it is already checked out.")
FileLocked = True
Err.Clear
End If
End Function
發生就行了錯誤Workbooks.Open FoundFiles(iIndex)
是什麼'FoundFiles'呢?在這個功能是問題... –