如何让 Excel 释放对我使用 ADO 访问过的另一个 Excel 文件的锁定?
我在 Excel 2003 文档中使用 VBA 宏通过 ADO 查询另一个 Excel 2003 文档。代码如下所示:
Dim vRecordSet As ADODB.Recordset
vWorkbookConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\SomeExcelFile.xls;Extended Properties=Excel 8.0;"
Set vRecordSet = New ADODB.Recordset
Call vRecordSet.Open(Source:=strSQl, ActiveConnection:=vWorkbookConnectionString, _
CursorType:=adOpenForwardOnly, LockType:=adLockReadOnly, Options:=adCmdText)
vRecordSet
稍后用于创建 PivotCache。
我的目标是当用户关闭运行此代码的工作簿时删除作为此 RecordSet 源的文件(上例中的 C:\SomeExcelFile.xls)。
运行此 ADO 查询时,该文件将被锁定,并且直到工作簿关闭后它似乎才会被释放。调用 vRecordSet.Close
不会释放它。
是否可以强制 Excel 释放对此文件的锁定,以便我可以通过编程方式删除它?
I'm using a VBA macro in an Excel 2003 document to query another Excel 2003 document via ADO. The code looks something like this:
Dim vRecordSet As ADODB.Recordset
vWorkbookConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\SomeExcelFile.xls;Extended Properties=Excel 8.0;"
Set vRecordSet = New ADODB.Recordset
Call vRecordSet.Open(Source:=strSQl, ActiveConnection:=vWorkbookConnectionString, _
CursorType:=adOpenForwardOnly, LockType:=adLockReadOnly, Options:=adCmdText)
vRecordSet
is later used to create a PivotCache.
My goal is to delete the file that is the source of this RecordSet (C:\SomeExcelFile.xls in the example above) when the user closes the workbook from which this code is run.
The file gets locked when this ADO query is run, and it doesn't seem to get released until after the Workbook is closed. Calling vRecordSet.Close
does not release it.
Is it possible to force Excel to release its lock on this file so that I can delete it programmatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试在
vRecordSet.Close
调用后添加以下内容以查看是否有所不同:Did you try adding the following after your
vRecordSet.Close
call to see if it makes a difference:未经测试的 Voodoo:使用一个变量来保存 ADODB.Connection,通过它来打开、关闭记录集和连接。
Untested Voodoo: use a variable to hold the ADODB.Connection, pass this to open, close both the recordset and the connection.