Thank you Peter.
I've found out that:
moExcelApp.ActiveWorkbook.Close SaveChanges:=False
does work.
But I think I prefer your solution (again
)
moExcel2ndTask.Displayalerts = False
moExcel2ndTask.Workbooks.Close
It's not a matter of preference. The examples I posted will close ALL
workbooks without saving, which is what you asked for.
..ActiveWorkbook.Close
merely closes the "activeworkbook", but no others
As yours is an automated instance which you maintain as invisible, and the
user won't touch it(?), you can be pretty confident if you only opened or
created one workbook you only need to close the single activeworkbook.
I do not need this:
because that code is executed when my app disconnects from Excel.
OK, if you are sure you won't need any other unexpected yet possibly useful
messages.
I am still confused about all these workbooks in Excel.
I am afraid for example that doing:
moExcel2ndTask.ActiveWorkbook.Close SaveChanges:=False
may not close all the books, or will it do it?
No it won't as explained above. But surely you will know how many workbooks
exist in your instance. When you create an automated instance no workbooks
which normally load on start-up will load, such as Addins, Personal, or the
default empty workbook. If you know there is only one non-hidden workbook
"it will do it". But if you have opened others or are in doubt use either of
the close-all approaches as previously posted.
moExcel2ndTask is created by my app only
Set moExcel2ndTask = CreateObject("Excel.Application")
moExcel2ndTask.Workbooks.Open (moExcelApp.ActiveWorkbook.FullName)
in difference to:
moExcelApp
which may be created (and opened) by my app or it could be already opened by
the user before my app starts.
Which invites another question:
Should my code close the workbook if it was not opened by my app?
I'm confused. I thought you said the workbook was opened by you in your
automated instance (invisible to user). If so it's entirely up to you when
you close it with or without saving changes (though if it's a ReadOnly copy
of a workbook already open in another instance you wouldn't be able to save
it anyway).
Normally you should not close a workbook that was opened by a user, unless
there is some very good reason in a particular enviroment or scenario.
Regards,
Peter T