'UTF-8、UTF-8(BOM付き)⇒Shift-JIS
Sub TEST1()
'変換したいテキストファイルのファイルパスを作成
Dim FilePath
FilePath = ThisWorkbook.Path & "\TEST.txt"
'UTF-8もしくはUTF-8(BOM付き)のテキストファイルを読み込み
With CreateObject("ADODB.Stream")
.Charset = "UTF-8"
.Open
.LoadFromFile FilePath
a = .ReadText
.Close
End With
'UTF-8もしくはUTF-8(BOM付き)以外を読み込んでしまった場合は終了
For i = 1 To Len(a)
If Mid(a, i, 1) <> Chr(63) Then
If Asc(Mid(a, i, 1)) = 63 Then
Exit Sub
End If
End If
Next
'改行毎にデータを分ける
b = Split(a, vbLf)
'Shift-JIS形式でテキストファイルへ出力
Open FilePath For Output As #1
For i = 0 To UBound(b)
Print #1, b(i)
Next
Close #1
End Sub
'Shift-JIS⇒UTF-8
Sub TEST2()
'変換したいテキストファイルのファイルパスを作成
Dim FilePath
FilePath = ThisWorkbook.Path & "\TEST.txt"
'Shift-JIS形式のテキストファイルを取得
a = ""
i = 0
Open FilePath For Input As #1
'テキストをすべて取得する
Do Until EOF(1)
Line Input #1, buf
a = a & buf & vbLf
Loop
Close #1
'Shift-JIS形式のファイル以外を取得した場合は終了
For i = 1 To Len(a)
If Asc(Mid(a, i, 1)) = -7295 Then Exit Sub
Next
'BOMを削除する
With CreateObject("ADODB.Stream")
.Charset = "UTF-8"
.Open
.WriteText a
.Position = 0
.Type = 1
.Position = 3
byteTmp = .Read
.Close
End With
'UTF-8でテキストファイルへ出力する
With CreateObject("ADODB.Stream")
.Charset = "UTF-8"
.LineSeparator = 10
.Type = 1
.Open
.Write byteTmp
.SetEOS
.SaveToFile FilePath, 2
.Close
End With
End Sub
'UTF-8(BOM付き)⇒UTF-8
Sub TEST3()
'変換したいテキストファイルのファイルパスを作成
Dim FilePath
FilePath = ThisWorkbook.Path & "\TEST.txt"
'UTF-8形式のテキストファイルを読み込み
With CreateObject("ADODB.Stream")
.Charset = "UTF-8"
.Open
.LoadFromFile FilePath
a = .ReadText
.Close
End With
'UTF-8もしくはUTF-8(BOM付き)以外を読み込んでしまった場合は終了
For i = 1 To Len(a)
If Mid(a, i, 1) <> Chr(63) Then
If Asc(Mid(a, i, 1)) = 63 Then
Exit Sub
End If
End If
Next
'BOMを削除する
With CreateObject("ADODB.Stream")
.Charset = "UTF-8"
.Open
.WriteText a
.Position = 0
.Type = 1
.Position = 3
byteTmp = .Read
.Close
End With
'UTF-8でテキストファイルへ出力する
With CreateObject("ADODB.Stream")
.Charset = "UTF-8"
.LineSeparator = 10
.Type = 1
.Open
.Write byteTmp
.SetEOS
.SaveToFile FilePath, 2
.Close
End With
End Sub
'Shift-JIS⇒UTF-8(BOM付き)
Sub TEST4()
'変換したいテキストファイルのファイルパスを作成
Dim FilePath
FilePath = ThisWorkbook.Path & "\TEST.txt"
'Shift-JIS形式のテキストファイルを読み込み
a = ""
Open FilePath For Input As #1
'テキストをすべて取得する
Do Until EOF(1)
Line Input #1, buf
a = a & buf & vbLf
Loop
Close #1
'Shift-JIS以外のファイルを読み込んでしまった場合は終了
For i = 1 To Len(a)
If Asc(Mid(a, i, 1)) = -7295 Then Exit Sub
Next
'UTF-8(BOM付き)でテキストファイルへ出力
With CreateObject("ADODB.Stream")
.Charset = "UTF-8"
.Open
.WriteText a, 0
.SaveToFile FilePath, 2
.Close
End With
End Sub
最初にShift-JIS形式でデータを取得しています。
次に読み込んだデータをUTF-8(BOM付き)形式でテキストファイルへ出力しています。
これでShift-JISをUTF-8(BOM付き)へ変換することができます。
VBAコードを実行してみます。
Shift-JIS形式のテキストファイルを用意しました。
Shift-JIS形式のテキストファイル
UTF-8(BOM付き)へ変換してみます。
Shift-JIS⇒UTF-8(BOM付き)へ変換
UTF-8(BOM付き)へ変換できました。
UTF-8をUTF-8(BOM付き)へ変換
UTF-8形式のテキストファイルをUTF-8(BOM付き)変換するVBAコードはこちらです。
'UTF-8⇒UTF-8(BOM付き)
Sub TEST5()
'変換したいテキストファイルのファイルパスを作成
Dim FilePath
FilePath = ThisWorkbook.Path & "\TEST.txt"
'UTF-8もしくはUTF-8(BOM付き)のテキストファイルを読み込み
With CreateObject("ADODB.Stream")
.Charset = "UTF-8"
.Open
.LoadFromFile FilePath
a = .ReadText
.Close
End With
'UTF-8もしくはUTF-8(BOM付き)以外を読み込んでしまった場合は終了
For i = 1 To Len(a)
If Mid(a, i, 1) <> Chr(63) Then
If Asc(Mid(a, i, 1)) = 63 Then
Exit Sub
End If
End If
Next
'UTF-8(BOM付き)でテキストファイルへ出力
With CreateObject("ADODB.Stream")
.Charset = "UTF-8"
.Open
.WriteText a, 0
.SaveToFile FilePath, 2
.Close
End With
End Sub