20
20

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を書き出す方法

Posted at

#VBAは標準ではShift-JISかUTF16LEのみ
マクロを使ってHTMLの出力をする際にUTF-8で書き出したかったのですが、WriteLine()関数などでは基本的にShift-Jisになるようです。

#UTF-8を書き出すにはADODB.Streamを使う(設定が必要)

Visual Basic Editor のメニューから[ツール]→[参照設定]を選び,[参照可能なライブラリファイル]の中から "Microsoft ActiveX Data Objects x.x Library" にチェックを入れます。

バージョンは最新のものでOK?

#サンプルコード
##書き出し用オブジェクトの準備

準備
Dim output As ADODB.Stream
Set output = New ADODB.Stream

With output
    .Type = adTypeText
    .Charset = "UTF-8"
    .LineSeparator = adLF
    .Open
End With

##書き出す内容の準備

HTML_code = ""
HTML_code = HTML_code & "<!DOCTYPE html> "
HTML_code = HTML_code & "<html> "
HTML_code = HTML_code & "<head> "
…
…

##書き出し&ファイル保存

  output.WriteText HTML_code, adWriteLine
  output.SaveToFile "保存先のディレクトリ/ファイル名", adSaveCreateOverWrite
  output.Close
20
20
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
20
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?