Saving file

B

broogle

Hi, I have a template (blank worksheet), I want to prevent the user to
override the template file. The code below will save the template as
different name (taken from cells). I don't know why everytime I press
save button, an error always comes up saying that '....The memory could
not be read' then it will close all the excel application. Any idea ? I
am using excel 2000. The code works fine if I saved it manually with
"file-save as". Thanks

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

Application.DisplayAlerts = False
Application.EnableEvents = False
ActiveWorkbook.SaveAs Filename:= _
"C:\Temp" & "\" & Range("c10") & Range("c7") & ".xls" _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

Dim Msg, Response
Msg = "File was saved as C:\Temp\" & Range("c10") & Range("c7") &
".xls"
Response = MsgBox(Msg, vbExclamation")

Application.DisplayAlerts = True
Application.EnableEvents = True

End Sub
 
T

Tom Ogilvy

Try setting Cancel = true

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

Application.DisplayAlerts = False
Application.EnableEvents = False
ActiveWorkbook.SaveAs Filename:= _
"C:\Temp" & "\" & Range("c10") & Range("c7") & ".xls" _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

Dim Msg, Response
Msg = "File was saved as C:\Temp\" & _
Range("c10") & Range("c7") & ".xls"
Response = MsgBox(Msg, vbExclamation")
Cancel = True
Application.DisplayAlerts = True
Application.EnableEvents = True

End Sub
 

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