I have an excel file, where we have created a custom sub that, when saving, presents the user with a Save As dialog that limits the list of available file types a user can save as. This has been used by a company for several years as a template - so they have hundreds of excel files saved in various locations with the following macro in it:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
' If user is doing a 'Save As', set the file type as "Excel Macro-Enabled Workbook",
' and set the list of available extensions they can save as.
Dim xFileName As String
Dim xFileExt As String
If SaveAsUI <> False Then
Cancel = True
xFileName = Application.GetSaveAsFilename("<job name goes here> CHANNEL SCHEDULE", "CSV UTF-8 (Comma delimited) (*.csv), *.csv," _
& "Excel Macro-Enabled Template (*.xltm), *.xltm," _
& "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm," _
& "Excel Workbook (MACROS DISABLED) (*.xlsx), *.xlsx," _
& "", 3, "Save As xlsm file")
If xFileName <> "False" Then
Application.EnableEvents = False
' Get the file extension.
xFileExt = Right(xFileName, Len(xFileName) - InStrRev(xFileName, "."))
' Save the file.
Select Case xFileExt
Case "csv"
ActiveWorkbook.SaveAs Filename:=xFileName, FileFormat:=xlCSV
Case "xltm"
ActiveWorkbook.SaveAs Filename:=xFileName, FileFormat:=xlOpenXMLTemplateMacroEnabled
Case "xlsm"
ActiveWorkbook.SaveAs Filename:=xFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
Case "xlsx"
ActiveWorkbook.SaveAs Filename:=xFileName, FileFormat:=xlOpenXMLWorkbook
Case Else
MsgBox "Error saving as the selected file type. Please try again, or try a different file type. Sorry."
End Select
Application.EnableEvents = True
Else
'MsgBox "Save As Operation Cancelled."
Cancel = True
Exit Sub
End If
End If
End Sub
That same file also happens to have "Always create backup" setting enabled as shown in the photo below, so that when any of these files are opened excel automatically creates a file in the same dir as the file just opened called "Backup of < file name you just opened >".
The problem is that now they need to disable this feature on all their excel documents, or at least give an end-user the ability to disable it themselves so they can do it manually.
The only way I have found to disable this feature is by going to Save As > Tools > General Options:
However, when using the Application.GetSaveAsFilename
method, the "General Options" option from the Tools dropdown is no longer available as shown below:
My questions:
- Is there another from the excel GUI to disable the "Always create backup" setting if they cannot get to it from their current Save As dialogs?
- Or is there another way to 'enable' the General Options dialog from the custom Save As dialog creaed by the
Application.GetSaveAsFilename
method? - Is there a way to programmatically change (disable) this backup setting?
CodePudding user response:
Thanks to VBasic2008 and Skin in the comments above.
Adding the CreateBackup:=False
flag to the save/save as process will disable the "Always create backup" setting.
So:
ActiveWorkbook.SaveAs Filename:=xFileName, FileFormat:=xlCSV
Changes to:
ActiveWorkbook.SaveAs Filename:=xFileName, FileFormat:=xlCSV, CreateBackup:=False