2013-06-12 308 views
0

我有一個捲起文件,可打開多個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)

+0

是什麼'FoundFiles'呢?在這個功能是問題... –

回答

0

當你給工作簿定義的名稱可能會出現此問題,然後多次複製工作表而不先保存並關閉工作簿,如在以下示例代碼:

Sub CopySheetTest() 
    Dim iTemp As Integer 
    Dim oBook As Workbook 
    Dim iCounter As Integer 

    ' Create a new blank workbook: 
    iTemp = Application.SheetsInNewWorkbook 
    Application.SheetsInNewWorkbook = 1 
    Set oBook = Application.Workbooks.Add 
    Application.SheetsInNewWorkbook = iTemp 

    ' Add a defined name to the workbook 
    ' that RefersTo a range: 
    oBook.Names.Add Name:="tempRange", _ 
     RefersTo:="=Sheet1!$A$1" 

    ' Save the workbook: 
    oBook.SaveAs "c:\test2.xls" 

    ' Copy the sheet in a loop. Eventually, 
    ' you get error 1004: Copy Method of 
    ' Worksheet class failed. 
    For iCounter = 1 To 275 
     oBook.Worksheets(1).Copy After:=oBook.Worksheets(1)   
    Next 
End Sub 

要解決此問題,保存並關閉週期性而複製過程正在發生的工作簿,如在以下示例代碼:

子CopySheetTest() 昏暗ITEMP作爲整數 昏暗oBook作爲工作簿 昏暗iCounter作爲整數

' Create a new blank workbook: 
iTemp = Application.SheetsInNewWorkbook 
Application.SheetsInNewWorkbook = 1 
Set oBook = Application.Workbooks.Add 
Application.SheetsInNewWorkbook = iTemp 

' Add a defined name to the workbook 
' that RefersTo a range: 
oBook.Names.Add Name:="tempRange", _ 
    RefersTo:="=Sheet1!$A$1" 

' Save the workbook: 
oBook.SaveAs "c:\test2.xls" 

' Copy the sheet in a loop. Eventually, 
' you get error 1004: Copy Method of 
' Worksheet class failed. 
For iCounter = 1 To 275 
    oBook.Worksheets(1).Copy After:=oBook.Worksheets(1) 
    'Uncomment this code for the workaround: 
    'Save, close, and reopen after every 100 iterations: 
    If iCounter Mod 100 = 0 Then 
     oBook.Close SaveChanges:=True 
     Set oBook = Nothing 
     Set oBook = Application.Workbooks.Open("c:\test2.xls") 
    End If 
Next End Sub 

來源 - 「MSDN」

+0

我不認爲這適用於我的情況,我在打開工作簿而不是複製工作簿時出現的錯誤。生病添加一些代碼到我原來的帖子 – user2478665

0

如果你明確你可能會得到更好的幫助和/或發佈更多的代碼。具體來說:1)你的主程序中的Workbooks.OpenFileLocked函數中的Open上出現錯誤? 2)FoundFiles()(您用於打開)和tempvar()(您用於關閉)之間的關係是什麼?你如何設置這些數組/變量?

沒有這些信息,這是我的最佳建議:在iIndex循環中使用工作簿變量。所以,你的循環之前添加

Dim wbLoop as Workbook 

然後代替

Workbooks.Open FoundFiles(iIndex) 

使用

Set wbLoop = Workbooks.Open(FoundFiles(iIndex)) 

和替代

With Workbooks(tempvar(iIndex - 1)) 

使用

With wbLoop 

和關閉您如果塊之前,加

Set wbLoop = Nothing 
+0

錯誤發生的行是Workbooks.Open FoundFiles(iIndex),tempvar is =到FoundFiles(iIndexs)我不知道爲什麼這樣做是這樣做的我沒有寫這個代碼,接縫對我來說完全沒有必要。儘管我現在已經解決了。我在服務器上ping文件的時間有時會超時。我認爲它最有可能的服務器沒有足夠快的響應當程序試圖打開一個文件。 – user2478665