カテゴリ

VBA/ASPなど

Option Explicit

' // ----------------------------------------------
' //      文字コード指定しテキストファイル作成
' //      ADO2.5以降が必要(ADODB.Streamオブジェクト)
' // ----------------------------------------------

Sub create_text_file( _
        strCharset As String, _
        LineSeparator As LineSeparatorEnum, _
        strFile As String, _
        strTxt As String _
    )

    Dim obj As ADODB.Stream
    objSet obj, "ADODB.Stream"
   
    With obj
        .Open
        .Charset = strCharset
        .LineSeparator = LineSeparator
       
        .WriteText replace_line_separator(strTxt, LineSeparator), adWriteChar
       
        .SaveToFile strFile, adSaveCreateNotExist
        .Close
    End With
   
   
    objUnSet obj

End Sub

 

' ======================
'   改行コード変換
' ======================
Private Function replace_line_separator( _
        strTxt As String, _
        LineSeparator As LineSeparatorEnum _
    ) As String

    Dim tmpRet As String
    Dim strLS As String
   
   
    Select Case LineSeparator
        Case adCRLF: strLS = vbCrLf
        Case adLF  : strLS = vbLf
        Case adCR  : strLS = vbCr
    End Select
   
   
    tmpRet = Replace(strTxt, vbNewLine, strLS)
    tmpRet = Replace(tmpRet, vbLf, strLS)
    tmpRet = Replace(tmpRet, vbCr, strLS)
   
    replace_line_separator = tmpRet
End Function
 

コメント