I'm creating a file with Excel VBA and everything is working but there is a empty line or carriage-return at the end I can't get rid of.
I am adding a carriage-return for each line when joining them back together but nothing I've tried will stop it from adding one at the end.
But even if I don't add any carriage-return there is still one at the end of the file.
This is the last row of a good file without the carriage-return or blank line at the end.
This is a bad file.
This is the script that writes the file.
I am adding a carriage-return for each line when joining them back together but nothing I've tried will stop it from adding one at the end.
VBA Code:
' Join the lines back together
modifiedContent = Join(lines, vbCrLf)
But even if I don't add any carriage-return there is still one at the end of the file.
Code:
' Join the lines back together
modifiedContent = Join(lines)
This is the last row of a good file without the carriage-return or blank line at the end.
This is a bad file.
This is the script that writes the file.
Code:
' Join the lines back together
modifiedContent = Join(lines, vbCrLf)
' Check if modifications were made
If fileContent <> modifiedContent Then
' Create an instance of ADODB.Stream
Set stream = CreateObject("ADODB.Stream")
' Specify the stream type (binary) and character set (UTF-8)
stream.Type = 2 ' adTypeText
stream.charset = "utf-8"
' Open the stream and write the content
stream.Open
stream.WriteText modifiedContent
' Save the content to the new file
stream.SaveToFile newFilePath, 2 ' adSaveCreateOverWrite
' Close the stream
stream.Close
' Clean up
Set stream = Nothing
MsgBox "The file has been successfully modified and saved as " & newFilePath
Else
MsgBox "No modifications were necessary."
End If