I'm working on stripping the carriage returns (but not the line feeds) from a .csv automatically with VBA. I figured out some Powershell syntax that will do the trick but I need to embed it within the VBA so there's just one file I can share with colleagues. I also need the Powershell syntax to be directed to the file path used by the rest of the macro. Here's the last part of the macro I've written that needs the final touches applied:
Private Sub Save()
' Save Macro
' Save as a .csv
Dim new_file As String
Dim Final_path As String
new_file = Left(Application.ActiveWorkbook.FullName, Len(Application.ActiveWorkbook.FullName) - 4) & ".csv"
ActiveWorkbook.SaveAs Filename:= _
new_file _
, FileFormat:=xlCSV, CreateBackup:=False
Final_path = Application.ActiveWorkbook.FullName
'Below, I need to pass the Final_path file path over to powershell as a variable then
'strip the carraige returns from the file and resave as the same .csv
End Sub
Next, I need the following Powershell script to be run (I also need the filepaths in the Powershell to be updated Final_path variable from VBA). To be clear, I need C:\Users\Desktop\CopyTest3.csv and .\DataOutput_2.csv to be updated to be equal to the Final_path variable from VBA. The following Powershell syntax clears carriage returns (\r) from a .csv file while leaving the line feeds (\n).
[STRING]$test = [io.file]::ReadAllText('C:\Users\Desktop\CopyTest3.csv')
$test = $test -replace '[\r]',''
$test | out-file .\DataOutput_2.csv
Thanks in advance for your help!