Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Workbook won't save

Hi All,

I have a worksheet with Customized BeforeSave and BeforeClose events.

The Customized BeforeSave event allows me to let the user save the workbook
and then output one of the sheets in xml format to a file with the same name
as the workbook but with a .xml extension.

The Customized BeforeClose event allows me to restore the toolbars to the
state they were in when the spreadsheet opened.

The problem is as follows.
IF I open the spreadsheet, change the data and then close the spreadsheet,
the message box appears saying "Do you want to save changes .....". I answer
yes.
Stepping through the code I find that the Me.Save statement in the
BeforeClose event triggers the BeforeSave event. So far so good.

Stepping through the BeforeSave event I get to the ThisWorkbook.Save
statement. The debugger shows this statement being executed but the workbook
does not save.
Executing ThisWorkbook.Save in the Immediate window has no effect either.
Allowing the code to continue running causes the workbook to close without
any changes being saved.

To reproduce this problem add the following code to the ThisWorbook module
of a blank spreadsheet. Save the workbook and close it. Open the workbook,
change some data somewhere and select close.


Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not Me.Saved Then
Msg = "Do you want to save the changes you made to "
Msg = Msg & Me.Name & "?"
Ans = MsgBox(Msg, vbQuestion + vbYesNoCancel)
Select Case Ans
Case vbYes
Me.Save
Me.Saved = True
Case vbNo
Me.Saved = True
Case vbCancel
Cancel = True
Exit Sub
End Select
End If
On Error Resume Next
'RestoreToolBars
ThisWorkbook.Saved = True
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Dim sFile

Application.EnableEvents = False
Cancel = True
On Error Resume Next
If SaveAsUI Then
Do
Err.Clear
sFile = Application.GetSaveAsFilename(ThisWorkbook.Name, "Excel
Files (*.xls), *.xls")
If Err.Number < 0 Then
Err.Raise (Err.Number)
GoTo Workbook_BeforeSave_Exit
End If
If sFile < False Then
ThisWorkbook.SaveAs sFile
Else
GoTo Workbook_BeforeSave_Exit
End If
Loop Until Err.Number = 0
Else
ThisWorkbook.Save
End If
' Call SaveAgRaterAsXML(sFile)
ThisWorkbook.Saved = True
Workbook_BeforeSave_Exit:
Application.EnableEvents = True
ThisWorkbook.Saved = True
Cancel = True
End Sub





Any help would be greatly appreciated.
Regards
Tony



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 94
Default Workbook won't save

Hi Tony,

Looks like you are cancelling out you action. It works if youchange the
last (second last row)

Cancel = True in the

Private Sub Workbook_BeforeSave

To

Cancel = False

Regards,
Bondi

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Workbook won't save

Thanks Bondi,

That did the trick, but I'm not quite sure why that doesn't cause the
workbook to be saved twice.
I though that after executing the Thisworkbook.save statement in the
BeforeSave event you would need to set Cancel = True to stop the workbook
from being saved again.

I would greatly appreciate if you could set me straight on this one.

Regards
Tony




"Bondi" wrote in message
oups.com...
Hi Tony,

Looks like you are cancelling out you action. It works if youchange the
last (second last row)

Cancel = True in the

Private Sub Workbook_BeforeSave

To

Cancel = False

Regards,
Bondi



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 94
Default Workbook won't save

Hi Toby,

I'm not that strong on the theory. I would like to help you get an
answer thou, since it got me puzzled..

I hope someone outthere can help us:)

Sorry to not straighten you out..

Regards,
Bondi

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shared workbook - to save or not to save bluebird Excel Discussion (Misc queries) 1 November 14th 06 06:17 PM
Select sheet tabs in workbook & save to separate workbook files stratocaster Excel Worksheet Functions 2 March 1st 06 03:35 PM
Using interop.excel to open a workbook, the workbook doesn't ask to save changes. [email protected] Excel Programming 1 December 28th 05 10:23 PM
Help on Workbook close and workbook save events Adam Harding Excel Programming 1 September 29th 05 04:12 PM
What commands do you use to name a workbook, save a workbook,open a workbook Steven R. Berke Excel Programming 1 July 24th 03 11:37 PM


All times are GMT +1. The time now is 09:07 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"