elmnas
05-26-2015, 01:07 AM
Hello guys I found this code on the forum
Could someone help me to add a regex expression
instead of
"change me" and "Changed!"
I want to search for "><"
and replace with ">\n\r<"
see code below
Sub CleanupXML()
Dim FSO As Object '//FileSystemObject
Dim ts(1) As Object '//TextStream
Dim s As String, t As String
Dim FileContents As String
'---------------------------------------------------------
Const SEARCH_FOR As String = "Change me" 'Search for ><
Const REPLACE_WITH As String = "Changed!" 'replace with >\n\r<
'---------------------------------------------------------
On Error GoTo ErrHandler:
s = Application.GetOpenFilename()
If s <> "False" Then
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(s) Then
'//Get File Contents
t = FSO.GetParentFolderName(s) & "\" & Replace(FSO.GetTempName(), ".tmp", ".xml")
Name s As t
Set ts(0) = FSO.OpenTextFile(t, 1, False, -2) '//For reading, use default encoding
FileContents = ts(0).ReadAll
ts(0).Close
Set ts(0) = Nothing
'//Make replacement
FileContents = Replace(FileContents, SEARCH_FOR, REPLACE_WITH)
'//Write new file contents
Set ts(1) = FSO.OpenTextFile(s, 2, True, -2) '//For writing, use default encoding
ts(1).Write (FileContents)
ts(1).Close
Set ts(1) = Nothing
'//Delete Temp file if all actions succeeded
FSO.DeleteFile (t)
End If
End If
'//Check that all files are closed
My_Exit:
If Not ts(0) Is Nothing Then
ts(0).Close
End If
If Not ts(1) Is Nothing Then
ts(1).Close
End If
Set FSO = Nothing
Exit Sub
ErrHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume My_Exit
End Sub
Thank you in advance
Could someone help me to add a regex expression
instead of
"change me" and "Changed!"
I want to search for "><"
and replace with ">\n\r<"
see code below
Sub CleanupXML()
Dim FSO As Object '//FileSystemObject
Dim ts(1) As Object '//TextStream
Dim s As String, t As String
Dim FileContents As String
'---------------------------------------------------------
Const SEARCH_FOR As String = "Change me" 'Search for ><
Const REPLACE_WITH As String = "Changed!" 'replace with >\n\r<
'---------------------------------------------------------
On Error GoTo ErrHandler:
s = Application.GetOpenFilename()
If s <> "False" Then
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(s) Then
'//Get File Contents
t = FSO.GetParentFolderName(s) & "\" & Replace(FSO.GetTempName(), ".tmp", ".xml")
Name s As t
Set ts(0) = FSO.OpenTextFile(t, 1, False, -2) '//For reading, use default encoding
FileContents = ts(0).ReadAll
ts(0).Close
Set ts(0) = Nothing
'//Make replacement
FileContents = Replace(FileContents, SEARCH_FOR, REPLACE_WITH)
'//Write new file contents
Set ts(1) = FSO.OpenTextFile(s, 2, True, -2) '//For writing, use default encoding
ts(1).Write (FileContents)
ts(1).Close
Set ts(1) = Nothing
'//Delete Temp file if all actions succeeded
FSO.DeleteFile (t)
End If
End If
'//Check that all files are closed
My_Exit:
If Not ts(0) Is Nothing Then
ts(0).Close
End If
If Not ts(1) Is Nothing Then
ts(1).Close
End If
Set FSO = Nothing
Exit Sub
ErrHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume My_Exit
End Sub
Thank you in advance