Use File as Template, Force Save as on Close

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
656
I have a file I want to make read only, so I selected that in the options tab. How do I change this code, ActiveWorkbook.Save, to force SaveAs so that the template is never written over? thanks.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I don't know if I should've noted that I have code in ThisWorkbook to remove menubar when the file closes.


VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim cbWSMenuBar As CommandBar

On Error Resume Next
    Set cbWSMenuBar = Application.CommandBars("Worksheet menu bar")
    cbWSMenuBar.Controls("Tool Menu").Delete

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
 
Upvote 0
You might consider to save your template as a real Excel template, so when it is accessed it opens as a yet unsaved file (Book1).

ScreenShot226.jpg
 
Upvote 0
OK, I updated the code and this forces the save as on close. I 'm going to try changing it to a template file, assuming I just then save with the same name to update it with changes I may make?

VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If Not SaveAsUI Then Exit Sub
    Cancel = True
End Sub

On Error GoTo ErrorHandler
   
    Dim FileName As String
    FileName = Application.GetSaveAsFilename(FileFilter:="Excel Macro-Enabled Workbook (*.xlsm), *.xlsm")
   
    If FileName = "False" Then Exit Sub
   
    Application.EnableEvents = False
    ThisWorkbook.SaveAs FileName:=FileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled

ErrorHandler:
    Application.EnableEvents = True
 
Upvote 0
Did you actually try it?
Most of the code is outside the before save event handler ;)
 
Upvote 0
to force SaveAs so that the template is never written over
That is not a guarantee, because the user can choose the same name.

I 'm going to try changing it to a template file, assuming I just then save with the same name to update it with changes I may make?
Yes, but be sure you OPEN the template instead of NEW'ing it.
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,792
Members
449,048
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top