VBA - Erreur format fichier CSV

Hello le forum,

Besoin d'un coup de main...

Voici mon problème, j'ai une macro (dans outlook) qui vient récupérer le dernier fichier télécharger, puis y prendre des données pour en former un fichier CSV.
Le problème étant, qu'en formant ce fichier CSV, mon fichier contient des guillements en début et fin. (Voir l'image ci-dessous)

image

Or, je viens charger ce fichier sur un site, mais forcément le format n'est pas accepter.
Quand j'ouvre le fichier CSV et que je double-clique sur une cellule contenu le texte, les guillemets disparaisse...

image

Et là je peux charger mon fichier.

J'ai essayer tout un tas de chose, que se soit depuis Outlook ou depuis Excel, mais rien n'y fait, je ne trouves malheureusement pas de solution.
Si quelqu'un sait quel est le problème et à une solution connu, je suis preneur 😊

Ci-dessous, vous trouverez mon code.

Merci d'avance !
Kilian

Sub CSV_Edit()
'Déclaration des variables
Dim appExcel As Excel.Application   'Application Excel
Dim wbDownloaded As Excel.Workbook, wbDHLFile As Excel.Workbook  'Classeur Excel
Dim wsDownloaded As Excel.Worksheet 'Feuille Excel
Dim sDHLFile$, sPath$, sDownloadedFile$ 'Lien
Dim InBox$, sCI$, sCopyPath$, sDescrArt$, sTxtCSV$, sNewDescrArt$ ' Texte
Dim MyData As New DataObject

On Error GoTo Error

InBox = InputBox("Etes-vous sur d'avoir extrait la CI de l'ERP ?" & Chr(10) & Chr(10) & "Si oui, merci d'indiquer le numéro de CI", "Numéro de CI.", "")

If InBox = "" Then
    MsgBox "Merci de renseigner un numéro de CI.", vbOKOnly
    Exit Sub
Else
    sCI = InBox
End If

' Chemin et nom du dernier fichier .xlsx télécharger
sDownloadedFile = LastFileNameOfDirectory(MyForumPath, "xlsx")

'Chemin du fichier
sPath = "MyForumPath"

'Nom du fichier
sDHLFile = sPath & sCI & ".csv"

'Ouverture de l'application
Set appExcel = CreateObject("Excel.Application")
appExcel.Visible = True
'Ouverture d'un fichier Excel
Set wbDownloaded = appExcel.Workbooks.Open(sDownloadedFile, 0, 0)
Set wbDHLFile = appExcel.Workbooks.Add

With wbDHLFile
For x = 2 To wbDownloaded.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
        sDescrArt = Replace(wbDownloaded.Sheets(1).Cells(x, 3), ",", " ")
        sTxtCSV = "1,INV_ITEM," & sDescrArt & ",8466.9430," & wbDownloaded.Sheets(1).Cells(x, 5) & ",EA," & _
        wbDownloaded.Sheets(1).Cells(x, 6) / wbDownloaded.Sheets(1).Cells(x, 5) & "," & wbDownloaded.Sheets(1).Cells(x, 7) & ",0.1,,CH,SON," & _
        wbDownloaded.Sheets(1).Cells(x, 2) & ","
        .Sheets(1).Cells(x - 1, 1) = RemoveSpecial(sTxtCSV)
Next
        .Sheets(1).Range("A1").Activate
        .SaveAs FileName:=sDHLFile, FileFormat:=xlCSV, CreateBackup:=False
        .Saved = True
        .Close
End With

wbDownloaded.Close

sCopyPath = InputBox("Le fichier est prêt et disponible à l'adresse : ", "Création CSV réussi", sDHLFile)

'If sCopyPath <> "" Then
'    MyData.SetText sCopyPath
'    MyData.PutInClipboard
'    MsgBox ("Adresse copiée dans le presse-papier !")
'End If

Exit Sub

Error:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbInformation, "Error " & Err.Number ' Affichage de l'erreur

End Sub

Hello,

Problème résolu, pour ceux qui aurait eu le même problème, voici la solution :

Dans mon code ci-dessous, je me suis obstiné à pré-remplir les données directement dans un format CSV, alors qu'en réalité, l'enregistrement en fichier CSV fera tout le travail.
J'ai donc insérer mes données 1 par colonne et lors de l'enregistrement, nous nous retrouvons avec le bon format.

Le code passe donc de ça :

With wbDHLFile
For x = 2 To wbDownloaded.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
        sDescrArt = Replace(wbDownloaded.Sheets(1).Cells(x, 3), ",", " ")
        sTxtCSV = "1,INV_ITEM," & sDescrArt & ",8466.9430," & wbDownloaded.Sheets(1).Cells(x, 5) & ",EA," & _
        wbDownloaded.Sheets(1).Cells(x, 6) / wbDownloaded.Sheets(1).Cells(x, 5) & "," & wbDownloaded.Sheets(1).Cells(x, 7) & ",0.1,,CH,SON," & _
        wbDownloaded.Sheets(1).Cells(x, 2) & ","
        .Sheets(1).Cells(x - 1, 1) = RemoveSpecial(sTxtCSV)
Next
        .Sheets(1).Range("A1").Activate
        .SaveAs FileName:=sDHLFile, FileFormat:=xlCSV, CreateBackup:=False
        .Saved = True
        .Close
End With

A ça :

With wbDHLFile
For x = 2 To wbDownloaded.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
        sDescrArt = Replace(wbDownloaded.Sheets(1).Cells(x, 3), ",", " ")
        sDescrArt = RemoveSpecial(sTxtCSV)
        .Sheets(1).Cells(x - 1, 1) = 1
        .Sheets(1).Cells(x - 1, 2) = "INV_ITEM"
        .Sheets(1).Cells(x - 1, 3) = sDescrArt
        .Sheets(1).Cells(x - 1, 4) = "8466.9430"
        .Sheets(1).Cells(x - 1, 5) = wbDownloaded.Sheets(1).Cells(x, 5)
        .Sheets(1).Cells(x - 1, 6) = "EA"
        .Sheets(1).Cells(x - 1, 7) = wbDownloaded.Sheets(1).Cells(x, 6).Value / wbDownloaded.Sheets(1).Cells(x, 5).Value
        .Sheets(1).Cells(x - 1, 8) = wbDownloaded.Sheets(1).Cells(x, 7)
        .Sheets(1).Cells(x - 1, 9) = "0.1"
        .Sheets(1).Cells(x - 1, 10) = "CH"
        .Sheets(1).Cells(x - 1, 11) = "SON"
        .Sheets(1).Cells(x - 1, 12) = wbDownloaded.Sheets(1).Cells(x, 2)
Next
        .SaveAs FileName:=sDHLFile, FileFormat:=xlCSV, CreateBackup:=False
        .Saved = True
        .Close
End With

A+,
Kilian

Bonjour Kilian1906
et merci pour ton retour sur la solution

Rechercher des sujets similaires à "vba erreur format fichier csv"