Sub DetectEncoding()
Dim filePath As String
Dim fileNum As Integer
Dim bom(0 To 2) As Byte
Dim encoding As String
filePath = "C:\path\to\your\file.csv"
' ファイルをバイナリモードで開く
fileNum = FreeFile
Open filePath For Binary As #fileNum
' ファイルの最初の3バイトを読み取る
Get #fileNum, , bom
Close #fileNum
' バイトオーダーマークを確認してエンコーディングを判別
If bom(0) = &HEF And bom(1) = &HBB And bom(2) = &HBF Then
encoding = "UTF-8"
ElseIf bom(0) = &HFF And bom(1) = &HFE Then
encoding = "UTF-16 (LE)"
ElseIf bom(0) = &HFE And bom(1) = &HFF Then
encoding = "UTF-16 (BE)"
Else
encoding = "SJIS (またはその他)"
End If
MsgBox "このファイルのエンコーディングは " & encoding & " です。"
End Sub
More than 1 year has passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme