3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[VBA]UTF-8ファイルを出力する

Last updated at Posted at 2017-12-28

はじめに

ExcelのデータをUTF-8でエンコードして出力したかったのですが、ファイル操作関連のステートメントや FileSystemObjectで対応しているエンコードは限られておりUTF-8では出力できません。
ActiveX Data Objectsを利用することで、UTF-8などさまざまなエンコードに対応ができそうなので、関数を作成しました。

準備

Visual Basic Editor のメニューから、[ツール]→[参照設定]で、リストから参照設定する。
・Microsoft ActiveX Data Objects x.x Library (最新バージョンのみでOK)

実装

Public Sub SaveFile(ByVal filename As String, ByVal text As String, _
                    Optional ByVal enc As String = "UTF-8")
    
    'ファイル出力
    Dim output: Set output = New ADODB.Stream
    output.Type = adTypeText
    output.Charset = enc
    output.Open
  
    output.WriteText text
    output.SaveToFile filename, adSaveCreateOverWrite
    output.Close

End Sub
3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?