0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

vbaで出力したファイルの文字コードをUTF-8にして出力する

Posted at
Sub WriteTextToFileInUTF8()
    Dim filePath As String
    Dim textData As String
    Dim stream As Object

    ' マクロを実行したファイルと同じフォルダにファイルパスを指定
    filePath = ThisWorkbook.Path & "\output_utf8.txt"
    
    ' 書き込むテキストデータ
    textData = "This is a sample text written to the file in UTF-8."

    ' ADODB.Stream オブジェクトを作成
    Set stream = CreateObject("ADODB.Stream")
    
    ' ストリームの設定
    stream.Type = 2 ' テキストモード
    stream.Charset = "UTF-8" ' 文字コードをUTF-8に設定
    
    ' テキストを書き込み
    stream.Open
    stream.WriteText textData

    ' ファイルに保存
    stream.SaveToFile filePath, 2 ' 2は上書き保存を指定する定数

    ' ストリームを閉じる
    stream.Close
    
    ' オブジェクトの開放
    Set stream = Nothing

    MsgBox "Text has been written in UTF-8 to " & filePath
End Sub
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?