' Open the file for writing only if modifications were made
' If fileContent <> modifiedContent Then
' fileNumber = FreeFile
' Open filePath For Output As #fileNumber
' Print #fileNumber, modifiedContent
' Close #fileNumber
' MsgBox "The file has been successfully modified."
' Else
' MsgBox "No modifications were necessary."
' End If
' 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 file
stream.SaveToFile filePath, 2 ' adSaveCreateOverWrite
' Close the stream
stream.Close
' Clean up
Set stream = Nothing
MsgBox "The file has been successfully modified."
Else
MsgBox "No modifications were necessary."
End If
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
' -------------------------------------------------------------
' Domenic's code - to convert to NoBOM
stream.Position = 3 'skip byte order mark
Dim oStreamUTF8NoBOM As Object
Set oStreamUTF8NoBOM = CreateObject("ADODB.Stream")
With oStreamUTF8NoBOM
.Type = 1 'adTypeBinary
.Open
modifiedContent.CopyTo oStreamUTF8NoBOM
End With
' -------------------------------------------------------------
' Save the content to the file
oStreamUTF8NoBOM.SaveToFile filePath, 2 ' adSaveCreateOverWrite
' Close the stream
stream.Close
oStreamUTF8NoBOM.Close
' Clean up
Set stream = Nothing
Set oStreamUTF8NoBOM = Nothing
MsgBox "The file has been successfully modified."
Else
MsgBox "No modifications were necessary."
End If
modifiedContent.CopyTo oStreamUTF8NoBOM
stream.CopyTo oStreamUTF8NoBOM
' Open the file for reading
fileNumber = FreeFile
Open filePath For Input As #fileNumber
fileContent = Input$(LOF(fileNumber), fileNumber)
Close #fileNumber
' Open the file using ADODB stream
With stream
.charset = "UTF-8"
.Open
.LoadFromFile filePath
fileContent = .ReadText
.Close
End With