VBA错误:调用的对象已与其客户端断开连接

时间:2015-08-12 23:34:16

标签: vba excel-vba excel

我编写了以下代码来复制当前工作表中的内容以创建新工作簿,如果我不双击新创建的工作簿,则可以调用代码而不会出现任何错误,但是一旦我打开新创建的工作簿工作簿并关闭它,代码将抛出异常:

Automation error :
The object invoked has disconnected from its clients

请参阅下面的代码:

Private Sub saveReportContents(smInstance As String, companyCode As String, jobName As String, onb_grp As String)
    Dim fileName As String
    Dim wb As Workbook

    Set wb = Workbooks.Add
    ThisWorkbook.Sheets("Contacts").Activate
    ThisWorkbook.Sheets("Contacts").Copy before:=wb.Sheets(1)

    fileName = ThisWorkbook.Path & "\" & "TDL_CTI_Onboard_" & smInstance & "_" & companyCode & "_" & jobName & "_" & onb_grp & "_" & _
               Format(Now, "yyyymmdd_hhmmss") & ".xlsx"

    wb.SaveAs fileName
    For Each butt In wb.Sheets("Contacts").Buttons
        butt.Delete
    Next butt
    wb.Sheets("Contacts").Rows(1).EntireRow.Delete
    wb.Save
    wb.Close savechanges:=False

End Sub

将突出显示的错误行VBA代码编辑器是:

ThisWorkbook.Sheets("Contacts").Copy before:=wb.Sheets(1)

我不知道为什么,你们能告诉我原因以及如何避免这个问题吗?

0 个答案:

没有答案