Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Me.Worksheets(1).SaveAs "Path\FileName.csv", xlCSV
End Sub
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Static AlreadySaving As Boolean ' Prevent recursion
If AlreadySaving Then Exit Sub
' Get file and app information
Dim FF As XlFileFormat: FF = Me.FileFormat
Dim FN As String: FN$ = Me.FullName
Dim DA As Boolean: DA = Application.DisplayAlerts
AlreadySaving = True ' Prevent recursion
Application.DisplayAlerts = False
' Save as CSV, which would recurse
Me.Worksheets(1).SaveAs "path\name.csv", xlCSV
Me.SaveAs FN$, FF ' Save as original, which would recurse
Application.DisplayAlerts = DA ' Restore alerts
Cancel = True ' Prevent saving again
AlreadySaving = False ' Allow re-saving
End Sub
It works, sort of!. When I close the workbook it does save as CSV to the location I want but it doesnt close down altogether i. e I want it to close as a CSV to the specified location and also as an xlsm in its current location. Thanks.
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim oHiddenApp As New Application
Dim oWb As Workbook
Dim sPathName As String
sPathName = Left(FullName, Len(FullName) - 5)
SaveCopyAs sPathName & ".cvs"
With oHiddenApp
Set oWb = .Workbooks.Open(sPathName & ".cvs")
.Calculation = xlCalculationManual
.DisplayAlerts = False
.EnableEvents = False
oWb.SaveAs sPathName & ".cvs", xlCSV
oWb.Close False
.Quit
End With
Set oHiddenApp = Nothing
Set oWb = Nothing
End Sub
Try this and see if it works for you : (will save a CVS copy in the workbook folder)
Code:Option Explicit Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim oHiddenApp As New Application Dim oWb As Workbook Dim sPathName As String sPathName = Left(FullName, Len(FullName) - 5) SaveCopyAs sPathName & ".cvs" With oHiddenApp Set oWb = .Workbooks.Open(sPathName & ".cvs") .Calculation = xlCalculationManual .DisplayAlerts = False .EnableEvents = False oWb.SaveAs sPathName & ".cvs", xlCSV oWb.Close False .Quit End With Set oHiddenApp = Nothing Set oWb = Nothing End Sub
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'first save only.
On Error Resume Next
If Len(Dir(FullName)) = 0 Then
With Application
.DisplayAlerts = False
.EnableEvents = False
Save
Call SaveCVS
Application.OnTime Now, Me.CodeName & ".SaveCVS"
.EnableEvents = True
.DisplayAlerts = True
End With
End If
On Error GoTo 0
'subsequent saves.
Call SaveCVS
End Sub
Private Sub SaveCVS()
Dim oHiddenApp As New Application
Dim oWb As Workbook
Dim sPathName As String
sPathName = Left(FullName, Len(FullName) - 5)
SaveCopyAs sPathName & ".cvs"
With oHiddenApp
Set oWb = .Workbooks.Open(sPathName & ".cvs")
.Calculation = xlCalculationManual
.DisplayAlerts = False
.EnableEvents = False
oWb.SaveAs sPathName & ".cvs", xlCSV
oWb.Close False
.Quit
End With
Set oHiddenApp = Nothing
Set oWb = Nothing
End Sub