开发者

How to access Excel file when multiple instances of Excel application are shown as open in Task Manager?

I need to access an Excel file. The application says that it's impossible to access the referred 开发者_开发百科file. The problem is that it's not opened.

I realized that Task Manager contained some Excel instances, so I quit them.

The user doesn't want to do that. I need to do this by code.


Your question is not completely clear, but I think you're automating Excel from Access VBA and that after you're finished with Excel, it's not closing down properly and so the next time your Access VBA code runs, it can't open the file since it's alwready open.

If that is the case, you need to make sure that you close Excel properly in your VBA code. Without seeing your exact code, it's difficult to say for sure what you need to do, but it should be something along these lines (wb is a WorkBook, ws is a WorkSheet and app is the Application object):

wb.Save
wb.Close
app.Quit

Set ws = Nothing
Set wb = Nothing
Set app = Nothing

If on the other hand you meant that the Excel instance that has locked the file wasn't started by your code, then it becomes a bit more difficult. You can write code to kill all Excel processes currently running, but that could mean that you'd kill an instance of Excel that the user is currently using or that's being used by another automated process, so it would be quite dangerous unless you could say with 100% certainty that your application will always run on a dedicated computer not sure by anything else.


Looking up your other questions so far I as well assume you are automating Excel from Access using VBA. I assume you have some code along those lines

Dim objExcel As Excel.Application
Dim curWorkbook As Excel.Workbook

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True

Set curWorkbook = objExcel.Workbooks.Open("C:\Temp\Excel.xls")

As ho points out, the remaining Excel processes indicate that you have ended your code without closing the Excel file. It could be that you just forgot to do so or that the relevant code is not run (maybe because of an error; meanwhile you have affirmed my assumption in your comment). What is helpful is to make sure (at least during development) to always make your Excel visible (see my code example above) so you see possible errors that prevent your workbook from being saved and therefore Excel from being quit. For your final code (when you don't want to show Excel, i.e. don't makte it visible) you might want to check if the file a) was really saved, b) the workbook is closed and no other workbook remains open, e.g. like so

If objExcel.Workbooks.Count = 0 Then
   objExcel.Quit
Else
   [Problem. Try to find and solve the problem before ending the code]
End If

And you look into adding code to catch the error(s) and to make sure that excel is indeed ended, no matter what.

On Error GoTo ErrCleanupImport
[your normal Code]


ErrCleanupImport:
    [quit excel and clean up]

End Sub

HTH Andreas

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜