PDA

View Full Version : Solved: Close workbook and quit XL



Digita
03-23-2009, 10:34 PM
Guys,

I got a code below to open another workbook, update it and then close the original workbook without saving it and shut down XL.

Dim wBook As Workbook
On Error Resume Next
Set wipBook = Workbooks("WIP.xls")
If wBook Is Nothing Then

Workbooks.Open Filename:="C:\foldername\wip.xls"
Else

Set wipBook = Nothing
Windows("WIP.xls").Activate

End If

ActiveWorkbook.Close True
ThisWorkbook.Saved = True
Application.Quit

I already set the Thisworkbook's property Saved = True. Can someone explain why XL still asks if I want to save it?

Thanks in advance.



kp

GTO
03-23-2009, 11:30 PM
Hi kp,

I didn't test, but it would seem to me that the 'Workbooks.Open Filename:="C:\foldername\wip.xls" ' would always execute, as wBook is not Set.

That aside, does ThisWorkbook have anything happening in BeforeClose?

Mark

Digita
03-24-2009, 02:54 PM
Thanks Mark,

Good pickup. Indeed, I made a typo, the code should read as below:

Dim wBook As Workbook
On Error Resume Next
Set wBook = Workbooks("wip.xls")
If wBook Is Nothing Then

Workbooks.Open Filename:="C:\foldername\wip.xls"
Else

Set wBook = Nothing
Windows("WIP.xls").Activate

End If

ActiveWorkbook.Close True
ThisWorkbook.Saved = True
Application.Quit


After saving the wip workbook, the active workbook is Thisworkbook which I don't want to save. On exit XL, how can we suppress the prompt "Do you want to save"?

Thanks


kp

GTO
03-25-2009, 12:54 PM
Hey kp,

Either my head is 'thicker' than most days, or the way I read this is:

IF 'Wip.xls' is open; kill the reference to it, activate it, save it, then close it. (After which we save ThisWorkbook and Quit the app).

OR, (if) 'Wip.xls' is not open; we open 'Wip', then save and close it (w/o actually having made changes to it). (After which we save ThisWorkbook and Quit the app).

Are you sure you are not leaving out some details? Why are we opening Wip just to close it?

Maybe try (change the workbook name, I just grabbed an available one):
Sub ex()
Dim wBook As Workbook

On Error Resume Next
Set wBook = Workbooks("Airbus.xls")
On Error GoTo 0

If Not wBook Is Nothing Then
wBook.Close True
End If
Set wBook = Nothing
ThisWorkbook.Saved = True
Application.Quit
End Sub

Or:
Sub ex2()

On Error Resume Next
Application.Workbooks("Airbus.xls").Close True
On Error GoTo 0

ThisWorkbook.Saved = True
Application.Quit
End Sub

Now - as to the 'Do you want to Save?' alert: This works fine for me, so my question remains...

...That aside, does ThisWorkbook have anything happening in BeforeClose?

Off the top of my head, the only other thing I can think of is: do you have any other workbooks open when Application.Quit is executed?

Hope this helps,

Mark

Digita
03-26-2009, 05:20 AM
Hey Mark,

Sorry I missed your question in the earlier post. Yes, my original ThisWorkbook does have the beforeclose event. I guess the beforeclose script reverts thisworkbook.Saved property back to False and causes the prompt to pop up.

Oh well, I would have Application.enableevents = false to turn it off.

Thanks for the pointer Mark. Have a great day :hi:.

Regards


kp