尝试closures使用Access VBA导出的工作簿时出错

我正在尝试编写代码以使Excelclosures一个电子表格,Access刚刚导出的Access。 但是,我一直抛出一个错误,告诉我我的桌子不存在,即使我是直接的IT! 非常令人沮丧。 所有的帮助,非常感谢。 谢谢。

Private Sub Command12_Click() DoCmd.SetWarnings False Const xlFileName As String = "\\ct13nt003\mfg\SMT_Schedule_Files\SMT Line Progress Files\Test\SMT2Updated.xlsx" Dim xlapp As Object Dim wb As Object Set xlapp = CreateObject("Excel.Application") Set wb = xlapp.workbooks.Open(xlFileName) 'Delete Existing File First; then create new On Error Resume Next Kill xlFileName On Error GoTo 0 DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "SMT4Export", xlFileName, True wb.Close savechanges:=False Set wb = Nothing Set xlapp = Nothing DoCmd.SetWarnings True End Sub 

您在Excel中打开xlFileName 。 然后尝试在Excel中仍然打开该工作簿文件时, Kill xlFileName 。 所以Kill尝试失败,但您不知道这一事实,因为您通过使用On Error Resume Next抑制错误消息。

如果您想Kill它,您需要closuresExcel中的工作簿以释放locking。 另一方面,正如Gord已经提到的那样,在你的代码中没有任何迹象表明为什么在Excel中打开工作簿是有用的。

关于错误消息“运行时错误3001” – Microsoft Office Access数据库引擎找不到对象“SMT4Export”确保对象存在…“ ,请确保您的数据库包含名为SMT4Export的表或查询。

看来TransferSpreadsheet是目标。 我build议你避免现在创build一个Excel应用程序实例,看看你是否可以得到这个简化版本的代码工作…

 Private Sub Command12_Click() Const xlFileName As String = "\\ct13nt003\mfg\SMT_Schedule_Files\SMT Line Progress Files\Test\SMT2Updated.xlsx" DoCmd.SetWarnings True If Len(Dir(xlFileName)) > 0 Then Kill xlFileName End If DoCmd.TransferSpreadsheet acExport, _ acSpreadsheetTypeExcel12Xml, "SMT4Export", xlFileName, True End Sub