View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
ALEX ALEX is offline
external usenet poster
 
Posts: 493
Default closing opened workbook/app in memory

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