closing opened workbook/app in memory

A

Alex

How could I close a workbook in a case of an error or if it was opened by a
program and wasn't closed when I start the program again?
(I have many workbooks to be opened and closed.)
I'm opening a workbook as follows:
Dim xlapp As New Excel.Application

Set xlbook = xlapp.Workbooks.Open(strOurFileName, True)
logFlagApp = True ' setting a flag that it was opened
....
'********** In addition I'm checking whether it's opened by using the
following:
Function FileLocked(strFileName As String) As Boolean
On Error Resume Next

Open strFileName For Binary Access Read Lock Read As #1
Close #1

If Err.Number <> 0 Then
FileLocked = True
Err.Clear
End If

End Function
'*********************
.....
ErrorHandling_Err:
If Err Then
If logFlagApp = False Then

Else
xlapp.ActiveWorkbook.Save
xlapp.Quit
logFlagApp = False
End If
Else

Resume ErrorHandling_Exit
End If

When the program opened the workbook and didn't close it I'm trying to use
this by inserting it into the code during debugging:
Excel.ActiveWorkbook.Close
but it's not working. There is some mistake about an object. Somehow it was
working before.

Thanks,

Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top