2011-01-18 5 views
1

私は以下を持っていますExcel VBA私が使用するスクリプトExcel for Mac 2011 Excelファイルのすべてのワークシートを.csvファイルにエクスポートします。ワークシートをExcel 2011 for Mac 2011でUTF-8形式のCSVファイルとして保存するにはどうすればよいですか?

Sub save_all_csv() 
    On Error Resume Next 
    Dim ExcelFileName As String 
    ExcelFileName = ThisWorkbook.Name 
    For Each objWorksheet In ThisWorkbook.Worksheets 
     ExcelFileNameWithoutExtension = RemoveExtension(ExcelFileName) 
     CsvFileName = ExcelFileNameWithoutExtension & "__" & objWorksheet.Name & ".csv" 
     Application.DisplayAlerts = False 
     objWorksheet.SaveAs Filename:="data:storage:original_excel:" & CsvFileName, FileFormat:=xlCSV, CreateBackup:=False 
     Application.DisplayAlerts = True 
    Next 
    Application.DisplayAlerts = False 
    Application.Quit 
End Sub 
Function RemoveExtension(strFileName As String) 
    strFileName = Replace(strFileName, ".xlsx", "") 
    strFileName = Replace(strFileName, ".xls", "") 
    RemoveExtension = strFileName 
End Function 

問題は、彼らが保存することであるように私はPHP経由でUTF-8に変換するために取得することはできませんので、私はVBAがUTF-8形式でこれらのファイルを保存したいと思い西のMac OSローマ最初は

alt text

私は、Streamオブジェクトを経由してCreateObjectでVBAからテキストを保存するためのいくつかの解決策を見つけたが、それはapparently not possibile on the Mac to use CreateObject to write directly to filesです。

ワークシートをExcel 2011 for Mac 2011でUTF-8形式のCSVファイルとして保存するにはどうすればよいですか?

答えて

1

ファイルの作成を自分で行う必要があると思います。CreateObjectを使用してストリームを作成することはできません。しかし、Open、Writeなど(多くの例がありますが、 here's one)を使用して調べることをお勧めします。私は非常に小さな実験的なものを最初に作って、ちょうどどのエンコーディングが終わるのかを調べることにします。

クリス

1

私は最終的にはちょうどUTF-8エンコードにルーチンを書いて、同じ問題を抱えていました。このコードは、Mac:Excel 2011のMacBook Proで動作します。を使用して、この関数で作成したバイナリファイル出力のバイト配列を使用してください。私の場合は、Macの優れた音声シンセの使用を自動化するために、タイ語のスクリプトを処理しています。

Private Function UTF8Encode(b() As Byte) As Byte() 
    ' Function to convert a Unicode Byte array into a byte array that can be written to create a UTF8 Encoded file. 
    ' Note the function supports the one, two and three byte UTF8 forms. 
    ' Note: the MS VBA documentation is confusing. It says the String types only supports single byte charset 
    '   however, thankfully, it does in fact contain 2 byte Unicode values. 
    ' Wrote this routine as last resort, tried many ways to get unicode chars to a file or to a shell script call 
    ' but this was the only way could get to work. 
    ' RT Perkin 
    ' 30/10/2015 

    Dim b1, b2, b3 As Byte   ' UTF8 encoded bytes 
    Dim u1, u2 As Byte     ' Unicode input bytes 
    Dim out As New Collection  ' Collection to build output array 
    Dim i, j As Integer 
    Dim unicode As Long 

    If UBound(b) <= 0 Then 
     Exit Function 
    End If 

    For i = 0 To UBound(b) Step 2 
     u1 = b(i) 
     u2 = b(i + 1) 
     unicode = u2 * 256 + u1 

     If unicode < &H80 Then 
      ' Boils down to ASCII, one byte UTF-8 
      out.Add (u1) 
     ElseIf unicode < &H800 Then 
      ' Two byte UTF-8 
      ' Code path not tested 
      b1 = &H80 Or (&H3F And u1) 
      b2 = &HC0 Or (Int(u1/64)) Or ((&H7 And u2) * 4) 
      out.Add (b2) ' Add most significant byte first 
      out.Add (b1) 
     ElseIf unicode < &H10000 Then 
      ' Three byte UTF-8 
      ' Thai chars are in this range 
      b1 = &H80 Or (&H3F And u1) 
      b2 = &H80 Or (Int(u1/64)) Or ((&HF And u2) * 4) 
      b3 = &HE0 Or (Int(u2/16)) 
      out.Add (b3) ' Add most significant byte first 
      out.Add (b2) 
      out.Add (b1) 
     Else 
      ' This case wont arise as VBA strings are 2 byte. Which makes some Unicode codepoints uncodeable. 
     End If 

    Next 

    Dim outBytes() As Byte 
    ReDim outBytes(1 To out.Count) 
    For j = 1 To out.Count 
     outBytes(j) = CByte(out.Item(j)) 
    Next 

    UTF8Encode = outBytes 

End Function 
+0

ワウ!どうもありがとう!あなたは私の一日を作った。これは正解とマークする必要があります。 –

+0

うれしいことはあなたに役立った! –

関連する問題