How to force close the worksheet without having Excel prompt?

J

Jack

How to force close the worksheet without having Excel prompt?

Right now I use this:
moExcel2ndTask.Workbooks.Close
and the Excel prompt is:
"
Do you want to save changes to ...xls?
Microsoft Office Excel recalculates formulas when opening files saved by an
earlier version of Excel.
"
Thanks,
Jack
 
J

Joel

From
moExcel2ndTask.Workbooks.Close

to
moExcel2ndTask.Workbooks.Close savechanges = true

or
moExcel2ndTask.Workbooks.Close savechanges = false
 
J

Jack

I am using visual basic.
When I do:
moExcel2ndTask.Workbooks.Close savechanges = False
I have vbasic compile error:
"Wrong number of arguments or invalid property assignment"
Thanks,
Jack
 
P

Peter T

'Close' with the Workbooks collection should not have any arguments

To avoid that message could do

For Each wb in moExcel2ndTask.Workbooks
'' maybe
' If wb.Saved = False then ' ?
wb.Close False
next

or

moExcel2ndTask.Displayalerts = False
moExcel2ndTask.Workbooks.Close
moExcel2ndTask.Displayalerts = True


As in the example above, Close with a single workbook can have optional
arguments, however if naming an argument like this
.... savechanges = true
it needs a colon
Savechanges:= true

Regards,
Peter T
 
J

Jack

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

I do not need this:
moExcel2ndTask.Displayalerts = True

because that code is executed when my app disconnects from Excel.
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?

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?
Thanks,
Jack
 
P

Peter T

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.
Thanks,
Jack

Regards,
Peter T
 
J

Jack

Peter, thank you for being with me.
As you can see these:
Set moExcel2ndTask = CreateObject("Excel.Application")
moExcel2ndTask.Workbooks.Open (moExcelApp.ActiveWorkbook.FullName)
moExcel2ndTask is created by my app but it is an invisible copy of:
1.
previously opened workbook by the user
or
2.
any active workbook previously opened by the user (if not specific wkb is
specified in app preferences)
3.
or
the specific (set in app preferences) workbook which if is not already
opened (see 1 above) will be opened on startup by my app.

So, as you can see I have to deal with several screnarios in here.
Jack
 

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