Application de la syntaxe Powershell directement dans VBA

Nov 20 2020

Je travaille sur la suppression des retours chariot (mais pas des sauts de ligne) d'un .csv automatiquement avec VBA. J'ai trouvé une syntaxe Powershell qui fera l'affaire, mais je dois l'intégrer dans le VBA afin qu'il n'y ait qu'un seul fichier que je puisse partager avec mes collègues. J'ai également besoin que la syntaxe Powershell soit dirigée vers le chemin du fichier utilisé par le reste de la macro. Voici la dernière partie de la macro que j'ai écrite qui nécessite les touches finales appliquées:

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

Ensuite, j'ai besoin du script Powershell suivant à exécuter (j'ai également besoin que les chemins de fichiers dans le Powershell soient mis à jour la variable Final_path de VBA). Pour être clair, j'ai besoin que C: \ Users \ Desktop \ CopyTest3.csv et. \ DataOutput_2.csv soient mis à jour pour être égaux à la variable Final_path de VBA. La syntaxe Powershell suivante efface les retours chariot (\ r) d'un fichier .csv tout en quittant les sauts de ligne (\ n).

[STRING]$test = [io.file]::ReadAllText('C:\Users\Desktop\CopyTest3.csv') $test = $test -replace '[\r]','' $test | out-file .\DataOutput_2.csv

Merci d'avance pour votre aide!

Réponses

1 TimWilliams Nov 20 2020 at 01:06

Voici une façon de modifier les séparateurs de ligne dans un fichier texte en utilisant FSO pour lire le fichier et écrire une version modifiée avec la fin de ligne que vous fournissez:

Sub tester()
    SetLineEndings "C:\Users\blah\Desktop\Temp\temp.csv", vbLf
End Sub

Sub SetLineEndings(fpath As String, sep As String)
    Dim fIn, fOut, ext
    With CreateObject("scripting.filesystemobject")
        ext = .getextensionname(fpath)
        Set fIn = .OpenTextFile(fpath)
        Set fOut = .CreateTextFile(Replace(fpath, "." & ext, "_mod." & ext), True)
        Do Until fIn.atendofstream
            fOut.write fIn.readline & sep
        Loop
        fIn.Close
        fOut.Close
    End With
End Sub
BernardVoss Nov 20 2020 at 03:28
Public FSO As New FileSystemObject

Private Sub Save()
'
' Save Macro
' Save as a .csv
'
Dim new_file As String
Dim Final_path As String
Dim PS_text As String
Dim PS_run As Variant
new_file = Left(Application.ActiveWorkbook.FullName, Len(Application.ActiveWorkbook.FullName) - 4) & ".csv"
    
    ActiveWorkbook.SaveAs Filename:= _
        new_file _
        , FileFormat:=xlCSV, CreateBackup:=False

Final_path = new_file

Application.DisplayAlerts = False
Dim b
Set b = VBA.CreateObject("WScript.Shell")

'creating temporary shall script
Set a = FSO.CreateTextFile(ThisWorkbook.Path & "\temp.ps1")
'saving commands into the script
a.WriteLine ("[STRING]$test = [io.file]::ReadAllText('" & Final_path & "')") a.WriteLine ("$test = $test -replace '[\r]',''") a.WriteLine ("$test | out-file " & Final_path)
'executing the script
b.Run "powershell " & ThisWorkbook.Path & "\temp.ps1", vbNormalFocus
Application.DisplayAlerts = True

End Sub