I want to prevent user to save the workbook with the same name as it is opened with, and to offer SaveAs option.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If ThisWorkbook.Name = "abc" Then
Cancel = True
SaveAsUI = True
End If
Also tried
If ThisWorkbook.Name = "abc" Then SaveAsUI = True
This code doesn't work. SaveAs dialog doesn't appear.
Next try
If ThisWorkbook.Name = "abc" Then ThisWorkbook.ReadOnly = True
'Error - can't assign to read only property.
Re: forcing Word templates to save as new documents The secret is to save templates as templates, not documents. Open your "template" and select Save As... Choose Save As Type Word Template (. dotx, rather than the standard .
However, nearly all Windows-based programs allow you to Save As using two quick key combinations: Press Alt + F to open the file menu. Let go of all keys when the file menu appears. Press "A" to choose the Save As option.
One is on the Quick Access Toolbar, or we can press F12 and display the save as option or press the keyboard shortcut CTRL+S, which opens the “Save As” dialog box to save the file in the desired format path.
Click the File tab, and then click Save As. Give the new template a file name, select Word Template in the Save as type list, and then click Save. Note: You can also save the template as a Word Macro-Enabled Template (.
If you want to test for a particular filename only - say abc.xlsm
then the code below will stop the Save
(but pass SaveAs
) then set the ReadOnly
attribute to False so Save
can't be used again on this file in this session
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not SaveAsUI Then
If ThisWorkbook.Name = "abc.xlsm" Then
Cancel = True
ThisWorkbook.ChangeFileAccess Mode:=xlReadOnly
End If
End If
End Sub
I have two suggestions, but in order to know which is best you have to tell us more details about the surrounding code and how you open/create files etc.
Use a template. If you put your code in a template and add a new workbook, it cannot be saved without the SaveAs dialog.
Make the workbook read only at opening. This can be done in a lot of ways, depending on the design of your project (eg Workbooks.Open
with ReadOnly
parameter).
The other answer (read only or template) are both good suggestions
However, if you really want to code it, try this
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim fName As String
If ThisWorkbook.Name "abc.xlsm" Then
If Not SaveAsUI Then
fName = Application.GetSaveAsFilename(, "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm")
If fName = "False" Then
MsgBox "File NOT saved", vbOKOnly
Cancel = True
Else
Application.EnableEvents = False
ThisWorkbook.SaveAs Filename:=fName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
Application.EnableEvents = True
End If
End If
End If
End Sub
Note: coded for Excel 2007/2010 (If ThisWorkbook.Name "abc.xlsm" Then
)
You woill need to change if using Excel 2003
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With