Sub Checkfile()
Dim FSO As New FileSystemObject
Dim fsoFile As File
'Open an XML file to search for illegal characters
Set SourceFile = FSO.OpenTextFile("C:\TestWithInvalidCharcter.xml")
'Create a new XML file that would not contain new xml file
FSO.CreateTextFile ("C:\Test.xml")
fnum = FreeFile()
Open "C:\Test.xml" For Append As fnum
Do While Not SourceFile.AtEndOfStream
'replace any ascii code 240 with whitespace and print to the file
Print #fnum, TestRegExp("\240 040", SourceFile.ReadLine)
'replace any ascii code 040 with white space and print to the file
Print #fnum, TestRegExp("\040", SourceFile.ReadLine)
Loop
Close fnum
End Sub
Function TestRegExp(myPattern As String, myString As String)
'Create objects.
Dim objRegExp As RegExp
Dim objMatch As Match
Dim colMatches As MatchCollection
Dim RetStr As String
' Create a regular expression object.
Set objRegExp = New RegExp
'Set the pattern by using the Pattern property.
objRegExp.Pattern = myPattern
' Set Case Insensitivity.
objRegExp.IgnoreCase = True
'Set global applicability.
objRegExp.Global = True
'Test whether the String can be compared.
If (objRegExp.Test(myString) = True) Then
'Get the matches.
Set colMatches = objRegExp.Execute(myString) ' Execute search.
For Each objMatch In colMatches ' Iterate Matches collection.
RetStr = objRegExp.Replace(myString, " ")
Next
Else
RetStr = myString
End If
TestRegExp = RetStr
End Function
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Most Valuable Expert award recognizes technology experts who passionately share their knowledge with the community, demonstrate the core values of this platform, and go the extra mile in all aspects of their contributions. This award is based off of nominations by EE users and experts. Multiple MVEs may be awarded each year.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.