Skip to content

Instantly share code, notes, and snippets.

@Lycheejam
Last active November 24, 2017 12:51
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 Lycheejam/757c053219e64645846b916b0d1426e5 to your computer and use it in GitHub Desktop.
Save Lycheejam/757c053219e64645846b916b0d1426e5 to your computer and use it in GitHub Desktop.
Public Const CharCd = "UTF-8"
Function fileWriter(ByRef dtStr As String, ByRef filePath)
'stream用意
Dim adoSt As Object
Set adoSt = CreateObject("ADODB.Stream")
With adoSt
.Type = adTypeText 'テキストタイプを指定
.Charset = CharCd '文字コード指定
'ファイルオープン
'この時点では保存されないので内部的に一時ファイルができている?
.Open
.WriteText dtStr 'テキスト書き込み
.Position = 0 'キャレット位置を先頭に指定(戻す)
'ファイルタイプをバイナリに変更
.Type = adTypeBinary
'先頭3バイトがBOMの為、3バイト読み飛ばす。
.Position = 3
Dim bufStr As Variant '一時バッファ
'BOM無しバイナリデータを読み込む
'(ここで読み込むバイナリデータはテキストで書き込んだdtStrの中身)
bufStr = .Read()
'キャレット位置を先頭に戻す
.Position = 0
'BOM情報がない状態で書き込む
.Write bufStr
'行末の余計なデータを削除
.SetEOS
.SaveToFile filePath, adSaveCreateOverWrite
End With
adoSt.Close
Set adoSt = Nothing
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment