Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force SaveAs instead Save

Tags:

excel

vba

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.
like image 304
Alegro Avatar asked Sep 16 '12 09:09

Alegro


People also ask

How do you force a save as in Word?

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 .

How do I save as Save As?

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.

How do I use 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.

Can you save over a Word template?

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 (.


3 Answers

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
like image 194
brettdj Avatar answered Oct 23 '22 23:10

brettdj


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.

  1. Use a template. If you put your code in a template and add a new workbook, it cannot be saved without the SaveAs dialog.

  2. 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).

like image 3
Olle Sjögren Avatar answered Oct 23 '22 23:10

Olle Sjögren


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

like image 3
chris neilsen Avatar answered Oct 23 '22 22:10

chris neilsen