φ(..) ビボ~6 φ(..)

主にAccess、VBAに関する備忘録

NULL文字のせいでテキストファイルがインポートできないとき

NULL文字を空白に置き換える処理

Sub WriteNewTextFile()

    Dim characterArray() As Byte
    Dim fileLen As Long
    Dim strOrigFile As String
    Dim strNewFile As String
    Dim MyString As String
    Dim fs As Object
    'Change the path and the names of the files according to your requirement.
    strOrigFile = "<Full path of your original text file>"
    strNewFile = "<Full path of the new text file>"

    Set fs = CreateObject("Scripting.FileSystemObject")
    If (fs.FileExists(strOrigFile)) Then
    
        'Open the file and obtain the length
        Open strOrigFile For Binary As #1
        fileLen = LOF(1)
    
        'Read the file
        ReDim characterArray(fileLen) As Byte
        Get #1, , characterArray
        Close #1
    
        'The problem with the file occurs because the file contains null values that are embedded
        Dim i As Long
    
        For i = 1 To fileLen
        'If the character is a null value, change it to a blank space like Notepad does
            If (characterArray(i) = &H0) Then
                characterArray(i) = &H20
            End If
        Next i
    
    'Write the replacement file
        Open strNewFile For Binary As #1
        Put #1, , characterArray
        Close #1

        MsgBox "Completed"
    Else
        MsgBox "Provide valid path of the text file"
    End If
End Sub
文書番号: 872914 - Access 2003 または Access 2002 で Null 値を含むサイズの大きなテキスト ファイルのデータのインポートまたはデータへのリンクを実行しようとすると、エラー メッセージが表示される