:
VB Excel "ThisWorkbok" . " " " " .
:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ActiveWorkbook.SaveCopyAs ActiveWorkbook.FullName + ".csv"
End Sub
CSV.
, XLSX ( XLSM XLS), , ( ) ).
:
, . , , . . , " " :
Dim fInSaving As Boolean
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If fInSaving Then
Exit Sub
End If
fInSaving = True
Dim workbookName As String
Dim parentPath As String
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
If SaveAsUI Then
Dim result
result = Application.GetSaveAsFilename
If VarType(result) = vbBoolean Then
If CBool(result) = False Then
Exit Sub ' user cancelled the dialog box
End If
End If
workbookName = fs.GetFileName(result)
parentPath = fs.GetParentFolderName(result)
Else
workbookName = ActiveWorkbook.name
parentPath = ActiveWorkbook.path
End If
Dim index As Integer
index = InStr(workbookName, ".")
Dim name As String
name = Left(workbookName, index - 1)
' extension can be empty is user enters simply a name in the 'File / Save as' dialog
' so it is not computed (but hard-coded below)
' do not ask for confirmation to overwrite an existing file
Application.DisplayAlerts = False
' save a copy
ActiveWorkbook.SaveAs fs.BuildPath(parentPath, name & ".csv"), XlFileFormat.xlCSV
' Save the normal workbook in the original name
ActiveWorkbook.SaveAs fs.BuildPath(parentPath, name & ".xlsm"), XlFileFormat.xlOpenXMLWorkbookMacroEnabled
Cancel = True
Application.DisplayAlerts = True
fInSaving = False
End Sub
Private Sub Workbook_Open()
fInSaving = False
End Sub
, ActiveWorkbook.SaveAs = > , .