Microsoft Office Forum [ www.Office-Fragen.de ] >> READONLY <<

Microsoft Office 2003-2019 => Excel => Thema gestartet von: maximumm am März 21, 2012, 14:45:53 Nachmittag

Titel: Office 2003: CSV-Ausgabe im UTF-8 Format
Beitrag von: maximumm am März 21, 2012, 14:45:53 Nachmittag
Hallo,

ich muss per VBA eine CSV-Datei erstellen. Soweit kein Problem.
Besteht die Möglichkeit, die CSV-Datei im UTF-8 Format auszugeben?
Excel bietet ja eine versteckte Möglichkeit, das UTF-8 Format unter
-->Speichern unter --> Extras --> Weboptionen --> Registerkarte "Codierung" auf UTF-8 einzustellen.

Wie lässt sich aber dies nun per VBA die CSV-Datei in diesem Format erstellen?

Vielen Dank für eure Tipps

Max
Titel: Antw:Office 2003: CSV-Ausgabe im UTF-8 Format
Beitrag von: maninweb am März 22, 2012, 11:03:18 Vormittag
Hallo maximumm...

UTF-8 kannst Du mit VBA unter Zuhilfenahme des ADODB.Stream Objektes schreiben. Wobei dann aber das Zusammenstellen der Daten für das CSV per Code passieren müsste, z.B. in einer Schleife, um strData zu füllen. Anbei ein Beispiel, das eine Testdatei erstellt.
Code: Visual Basic
  1.   Option Explicit
  2.  
  3.   Public Function mlfpWriteUTF8() As Long
  4.    
  5.     Dim strPath As String
  6.     Dim strFile As String
  7.     Dim strData As String
  8.     Dim objData As Object
  9.    
  10. '   Bypass...
  11.    
  12.     On Error Resume Next
  13.    
  14. '   Initialize...
  15.    
  16.     strPath = ThisWorkbook.Path
  17.     strFile = "Text.csv"
  18.    
  19. '   Check...
  20.    
  21.     If Not Trim(UCase(Dir(strPath & "\" & strFile))) <> Trim(UCase(strFile)) Then
  22.      
  23. '     Kill...
  24.      
  25.       Kill strPath & "\" & strFile
  26.      
  27. '     Clear...
  28.      
  29.       Err.Clear
  30.      
  31.     End If
  32.    
  33. '   Check...
  34.    
  35.     If Trim(UCase(Dir(strPath & "\" & strFile))) <> Trim(UCase(strFile)) Then
  36.    
  37. '     Data...
  38.      
  39.       strData = ""
  40.       strData = strData & "Hallo" & ";"
  41.       strData = strData & "Welt"
  42.      
  43. '     Create...
  44.      
  45.       Set objData = CreateObject("ADODB.Stream")
  46.      
  47. '     Check...
  48.      
  49.       If Not objData Is Nothing Then
  50.        
  51. '       Settings...
  52.        
  53.         objData.Type = 2
  54.         objData.Charset = "utf-8"
  55.        
  56. '       Store...
  57.        
  58.         objData.Open
  59.         objData.writetext strData
  60.         objData.SaveToFile strPath & "\" & strFile, 2
  61.        
  62.       End If
  63.        
  64.     End If
  65.    
  66. '   Return...
  67.    
  68.     mlfpWriteUTF8 = 0
  69.    
  70.   End Function
  71.  

Zu beachten ist, dass es zwei UTF-8 Formate gibt, einmal mit BOM = Byte Order Mark und einmal ohne. Das Beispiel hier schreibt eine Datei mit BOM.

Gruß
Titel: Antw:Office 2003: CSV-Ausgabe im UTF-8 Format
Beitrag von: maximumm am März 22, 2012, 17:32:50 Nachmittag
Hallo maninweb,

prima, vielen Dank. Das sieht schon sehr gut aus.
Ich werde mal testen!

Grüße

Max