Link to home
Start Free TrialLog in
Avatar of johngr904
johngr904

asked on

How do remove or replace a TM symbol in an XML File

I have written a vba program in Excel that logs onto a website, navigates to an XML export file, and saves it to a network folder for processing by our AS/400.  The problem is that occasionally a trademark symbol chr(153) is in the file and that causes the file to bomb out on the AS/400 because it does not recognize the symbol.  Any ideas for a simple routine to remove the TM symbol or replace it with a space?  I keep having issues with importing it into a worksheet and changing it there.  It loses the XML format, mostly involving quote signs.
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Portion code from http://www.ozgrid.com/forum/showthread.php?t=53975&highlight=manipulate+text+file
Sub ChangeTextFile() 
     ' Manipulating a text file with VBA
     '  Loops through text file and creates revised one
     ' This code requires a  reference (Tools > References) to Microsoft Scripting Runtime
     
    Dim FSO As FileSystemObject 
    Dim FSOFile As TextStream, FSOFileRevised As TextStream 
    Dim FilePath As String, FilePathRevised As String 
     
    FilePath = "c:\test.xml" ' create a test.xml file or change this
     
     ' adds "_Revised" to your file name
    FilePathRevised = Left(FilePath, Len(FilePath) - 4) & "_Revised" & Right(FilePath, 4) 
     
    Set FSO = New FileSystemObject 
    If FSO.FileExists(FilePath) Then 
         ' opens the file for reading
        Set FSOFile = FSO.OpenTextFile(FilePath, 1, False) 
         ' opens "revised" file in write mode
        Set FSOFileRevised = FSO.OpenTextFile(FilePathRevised, 2, True) 
        Do While Not FSOFile.AtEndOfStream 
            FSOFileRevised.WriteLine (Replace(FSOFile.ReadLine, Chr(153), "TM"))
        Loop 
        FSOFile.Close 
        FSOFileRevised.Close 
    Else 
         MsgBox (FilePath & " does not exist") 
    End If 
End Sub 

Open in new window

This should work without having to add the referenec
Dim iSource As Integer, iDest As Integer, sText As String
''Close any open text files
Close
''Get the number of the next free text file
iSource = FreeFile
''Write the entire file to sText
Open "C:\input.xml" For Input As #nSourceFile
sText = Input$(LOF(1), 1)
Close
'set and open file for output
iDest = FreeFile
Open "C:\input2.xml" For Output As iDest
'write project info and then a blank line. Note the comma is required
Print #fnum, Replace(sText, Chr(153), "TM")
Close #fnum
End Sub

Open in new window

Avatar of johngr904
johngr904

ASKER

Neither Works.  They both run fine, but when I look at the revised file, the ™ symbols are still there.
What are you using to open the XML files anyway? Internet Explorer?

Instead of replacing what I thought is the raw character in the XML file, you probably have to replace this &153;

Print #fnum, Replace(sText, "&153;", "TM")

If you open the source xml from notepad, it should have &153; in it
Doesn't make any difference using the &153, and when I open the XML file with notepad it shows the ™ symbol.
It has something to do with the trademark symbol, because I can replace other text strings and it works fine, just not with the ™ symbol.
Trademark symbol can be Chr(153) or Unicode ChrW(2122).
Can you attach the original file that contains the ™ symbol?
Sure, here is a sample record in the file that contains a ™ symbol.  I also tried ChrW(2122), per your reply, but that didn't work either.  I had also tried ChrW(8482).
Sample-Batch-Record.xml
ASKER CERTIFIED SOLUTION
Avatar of Dana Seaman
Dana Seaman
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, that was just what I needed!  Couldn't find that information anywhere!