Thread: Crash on save
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
patrick molloy patrick molloy is offline
external usenet poster
 
Posts: 391
Default Crash on save

You assume that th eadded sheet is called sheet1. It may
not always be so if the user has a different default
template

replace:
If objSheet Is Nothing Then ThisWorkbook.Sheets.Add
with:
If objSheet Is Nothing Then
set objSheet = ThisWorkbook.Sheets.Add
objSheet.Name="Sheet1"
End If

This forces the sheet name to Sheet1 whatever its default
was. What does the variable g_blnIgnore do?
If you're trapping the workbooks SheetActivate event
(which your loop would trigger) then you can stop this
with :
Application.EnableEvents = False
before your FOR...Next loop


Patrick Molloy
Microsoft Excel MVP

-----Original Message-----
I have been pulling my hair out on this. Can anyone

tell me why Excel
2K would crash after the BeforeSave event. My situation

is this; I
have a workbook which has three sheet which all contain

template style
information. I do not want the user to ever be able to

overwrite
these template sheets. Normaly this is not a problem as

I have hidden
(read replaced) the menu so they (the user) does not

even get the
option. However when I make changes to the source and

try to do a
save excel correctly does my BeforeSave code then saves

the workbook
then dies. I have included my BeforeSave code below.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As

Boolean, Cancel As
Boolean)
Dim objSheet As Worksheet

Application.DisplayAlerts = False
Application.ScreenUpdating = False

'Make sure we have our place holder sheet
'NOTE: This sheet is required since we can not save
' a workbook with no sheets
On Error Resume Next
Set objSheet = ThisWorkbook.Sheets("Sheet1")
On Error GoTo 0
If objSheet Is Nothing Then ThisWorkbook.Sheets.Add

'Delete all sheets other than place holder sheet
'We save without any of the 'Special' sheets so that
'all that is being saved is the source code for this
'workbook and not the template sheets. When the
'workbook is reloaded the special sheets will be

added
'back in (See WorkBook.Open)
For Each objSheet In ThisWorkbook.Sheets
If objSheet.Name < "Sheet1" Then
g_blnIgnore = True
objSheet.Delete
g_blnIgnore = False
End If
Next

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

Any thoughs
Thanks
Steve
.