簡體   English   中英

對象“工作簿”的運行時錯誤1004方法“添加”失敗

[英]Run time error 1004 method 'add' of object 'workbooks' failed

我正在編寫一個用於輸入和輸出記錄的股票登記簿的代碼。 幾天后,我才剛完成,每次測試都運行良好。 但是,今天,當我完成完整的代碼后,嘗試打開文件.xlsm進行更改注冊時遇到了問題。

錯誤信息:

運行時錯誤1004對象“工作簿”的方法“添加”失敗

在這里,您可以找到excel向我顯示錯誤消息的子例程。 本質上,要調試的突出顯示的行是Set book = app.Workbooks.Add(FileOUT)

    Sub Register_Deleted_Location(nData As Integer, DATA_ARRAY As Variant, User As String, PATH_HISTORY As String, HISTORY_FILE As String, TAB_HISTORY As String, RegError As Boolean)
'
    'Getting the number of columns for the data in the file
    Call Definition_Columns
    '
    RegError = False
    '
    'Open HISTROY_FILE
    Dim app As New Excel.Application
    Dim book As Excel.Workbook
    Dim FileOUT As String, PathOUT As String, TAB_OUT As String
    '
    app.Visible = False 'Visible is False by default, so this isn't necessary
    '
    FileOUT = PATH_HISTORY & "\" & HISTORY_FILE
    '
    If (Not FileExists(FileOUT)) Then
        MsgBox ("File does not exist in the Path" & vbNewLine & vbNewLine & FileOUT & vbNewLine & vbNewLine & "Please contact your BSO.")
        RegError = True
        GoTo End_Reg_Del
    End If
    If (FileLocked(FileOUT)) Then
        MsgBox ("File is locked, it is probably that another user is updating it, please try again in few seconds." & vbNewLine & vbNewLine & FileOUT & vbNewLine & vbNewLine & "If the problem persists, please contact your BSO")
        RegError = True
        GoTo End_Reg_Del
    End If

    Set book = app.Workbooks.Add(FileOUT)
    '
    TAB_OUT = TAB_HISTORY
    '
    'Unprotect sheet in HISTROY_FILE
    book.Sheets(TAB_OUT).Unprotect
    '
    'Looking for the first empty row in order to add the new register
    FER = First_Empty_Row_InColumn(book, TAB_OUT, "A", First_Row_For_Registers, 10000)
    '
    'Update information in HISTROY_FILE
    For i = 1 To nData
        book.Sheets(TAB_OUT).Cells(FER, i).Value = DATA_ARRAY(i)
    Next i
    '
    'Register the Date/Time/User Deleted
    book.Sheets(TAB_OUT).Cells(FER, Col_DTD).Value = Format(Now(), "DD/MM/YYYY")
    book.Sheets(TAB_OUT).Cells(FER, Col_TID).Value = Format(Now(), "HH:MM:SS")
    book.Sheets(TAB_OUT).Cells(FER, Col_USD).Value = User
    '
    'Fixing the problem with the date after copy the date from another file
    book.Sheets(TAB_OUT).Cells(FER, Col_DTC).NumberFormat = "DD/MM/YYYY"
    'book.Sheets(TAB_OUT).Cells(FER, Col_TIC).NumberFormat = "HH:MM:SS"
    book.Sheets(TAB_OUT).Cells(FER, Col_DTM).NumberFormat = "DD/MM/YYYY"
    'book.Sheets(TAB_OUT).Cells(FER, Col_TIM).NumberFormat = "HH:MM:SS"
    '
    'Update Date/Time of the last modification in HISTORY_FILE
    book.Sheets(TAB_OUT).Cells(Row_DT_File, Col_DT_File).Value = Format(Now(), "DD/MM/YYYY") & "  " & Format(Now(), "HH:MM:SS")
    '
    Call Sorting_File(book, TAB_OUT, "AX", "AY")
    '
    'Protect sheet in STOCK OUT file
    book.Sheets(TAB_OUT).Protect
    '
    'Save and close STOCK OUT File
    app.DisplayAlerts = False
    book.SaveAs Filename:=FileOUT, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    app.DisplayAlerts = True
    book.Close SaveChanges:=True
    app.Quit
    '
End_Reg_Del:
End Sub

有人可以幫助我了解此錯誤以及如何解決該錯誤嗎? 我花了整整一天的時間試圖通過Google找到答案,但此刻沒有任何幫助。

我將不勝感激。

最好的祝福,

您的fileOut字符串可能有問題。 “ .Add”僅接收一個參數,即模板文件。 如果失敗,則參數可能不正確(文件地址可能無效或文件本身不是模板)。

將值分配給fileOut字符串后,停止代碼並檢查其值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM