View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson[_2_] Dave Peterson[_2_] is offline
external usenet poster
 
Posts: 420
Default How can I force a workbook to close?

I've never seen excel open multiple files with the same name in the same
instance. And I bet if you check again, there's either a difference in names or
you have multiple instances open.

I'm not sure what you're doing, but if don't want to close excel, you could
check to see how many workbooks are open in the _BeforeClose event.

If Workbooks.Count 2 Then

Might be sufficient to check???

On 07/22/2010 13:07, ryguy7272 wrote:
How can I force a workbook to close?

I run this code:
ThisWorkbook.EnableAutoRecover = False
ThisWorkbook.Saved = True
ThisWorkbook.Close

That triggers this:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.name = "MainFile.xls" Then
Application.Quit
End If
End Sub

But that kills the whole Excel-application! If I open Excel and then
goto Start Office Excel, I have 2 instances of Excel open. I can
end one instance pretty easy. However, if I have two workbooks open
within the SAME instance, Excel doesn’t seem to be able to close one
file and keep the other open. In fact, Excel opens several new files,
all named ‘MainFile.xls’. VERY ANNOYING!!!

How can I shut down any file named ‘MainFile.xls’ and keep other Excel
files open?

Thanks!
Ryan--


--
Dave Peterson