运行时错误1004方法'添加'对象'工作簿'失败

时间:2018-02-28 19:58:40

标签: excel vba runtime-error

我正在为输入和输出记录的库存寄存器编写代码。我刚刚完成几天后就完成了,而且每次测试它都能完美地工作。但是,今天我完成完整的代码后,我只是试图打开一个.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

有人可以帮助我理解这个错误以及如何解决它吗? 我花了一整天的时间试图通过谷歌找到答案,但此刻没有任何帮助。

我会很感激任何提示。

致以最诚挚的问候,

1 个答案:

答案 0 :(得分:0)

您的fileOut字符串可能有问题。 “.Add”只接收一个参数,即模板文件。 如果失败,可能是参数不正确(文件地址可能无效或文件本身不是模板)。

在为fileOut字符串赋值并检查其值后停止代码。