Skip to content

Instantly share code, notes, and snippets.

@honda0510
Last active September 24, 2015 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save honda0510/805649 to your computer and use it in GitHub Desktop.
Save honda0510/805649 to your computer and use it in GitHub Desktop.
【VBA】文字エンコーディング変換関数
' 参照設定
' Microsoft ActiveX Data Objects 2.x Library
Function ConvCharSet(from_bin, from_charset, to_charset)
Dim from_stm As ADODB.Stream
Dim to_stm As ADODB.Stream
' バイナリ型のStreamオブジェクトを作成
Set from_stm = New ADODB.Stream
from_stm.Open
from_stm.Type = adTypeBinary
from_stm.Write from_bin
from_stm.Position = 0
from_stm.Type = adTypeText
from_stm.Charset = from_charset
' テキスト型のStreamオブジェクトを作成
Set to_stm = New ADODB.Stream
to_stm.Open
to_stm.Type = adTypeText
to_stm.Charset = to_charset
' to_charsetに変換されてコピーされる
from_stm.CopyTo to_stm
to_stm.Position = 0
to_stm.Type = adTypeBinary
If to_charset = "UTF-8" Then
to_stm.Position = 3
End If
' 文字コード変換後のバイナリを返す
ConvCharSet = to_stm.Read
from_stm.Close
to_stm.Close
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment