导出后锁定的Excel文件

时间:2014-05-07 12:29:49

标签: access-vba

我在下面的代码中遇到了一些问题。从访问excel的数据导出数据很好,但是,当我去查看excel文件时,它说它已被锁定以进行编辑。有谁知道为什么?

Public Sub ExportFiles()

Dim X As Object
Dim Y As Object
Dim XL As Object
Dim rs As Recordset

Set X = CreateObject("Excel.Application")
Set Y = X.Workbooks.Open("\\POISE\Data\LCS\DomGroup\ICE NMW Performance and DQ\Tasking MI\2014-15\Production Pack Template\OFFICIAL SENSITIVE Tasking Team Production Pack.xlsx")
Set XL = Y.Sheets("Tasking Records")
Set rs = CurrentDb.OpenRecordset("ALL")

XL.Range("A2").CopyFromRecordset rs

Y.SaveAs "\\POISE\Data\LCS\DomGroup\ICE NMW Performance and DQ\Tasking MI\2014-15\Production Pack Output\" & "Tasking " & "Week " & Format(FiscalWeek, "w") & Format(Date, "yyyymmdd") & ".xlsx"

X.Visible = False

Set X = Nothing
Set Y = Nothing
Set XL = Nothing
rs.Close
Set rs = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

您没有正确关闭Excel应用程序。在将对象设置为空之前,您需要关闭工作簿并退出excel。

有关详细信息,请参阅my answerRemove Excel Task from Task Manager after running vba

Public Sub ExportFiles()

Dim X As Object
Dim Y As Object
Dim XL As Object
Dim rs As Recordset

    Set X = CreateObject("Excel.Application")
    Set Y = X.Workbooks.Open("\\POISE\Data\LCS\DomGroup\ICE NMW Performance and DQ\Tasking MI\2014-15\Production Pack Template\OFFICIAL SENSITIVE Tasking Team Production Pack.xlsx")
    Set XL = Y.Sheets("Tasking Records")
    Set rs = CurrentDb.OpenRecordset("ALL")

    XL.Range("A2").CopyFromRecordset rs

    Y.SaveAs "\\POISE\Data\LCS\DomGroup\ICE NMW Performance and DQ\Tasking MI\2014-15\Production Pack Output\" & "Tasking " & "Week " & Format(FiscalWeek, "w") & Format(Date, "yyyymmdd") & ".xlsx"

    X.Visible = False

    ' remove objects in reverse order to creation
    Set XL = Nothing
    ' close workbook before quitting excel
    Y.Close
    Set Y = Nothing
    'close excel before setting it to nothing
    X.Quit
    Set X = Nothing


    rs.Close
Set rs = Nothing