Write File Adding Extra Tyte

  • Thread starter Thread starter DING
  • Start date Start date
D

DING

I used the code for changing NULLS to Spaces from a KnowledgeBase article.
However, when I write out the edited byte character array it is adding one
byte to the end of my file, causing problems for the program that it is
intended for input for.

Any idea why it's adding a null at the end of the character array and how I
can get it to NOT do so?
 
I used the code for changing NULLS to Spaces from a KnowledgeBase article.
However, when I write out the edited byte character array it is adding one
byte to the end of my file, causing problems for the program that it is
intended for input for.

Any idea why it's adding a null at the end of the character array and how I
can get it to NOT do so?
Please post your code. We can't fix what we can't see.
 
Sub WriteNewTextFile(strOrigFile As String, strNewFile As String)
'
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 = InputBox("Input Path And Name Of Existing File", "Input
File", "\\PACONFPP0001\FTPRoot\Actuarial\NWI.NWIVAL.PPDH05")
' strNewFile = InputBox("Enter Full path of the new text file", "Output
File", "C:\PolySystems\200909_GAAP\PDH\F05.UPD")
'
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
Dim j As Long
'
j = 0
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
j = j + 1
End If
Next i
Debug.Print i, j
'
'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
 
Back
Top