Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing newline charcters in a text file using VBA

Status
Not open for further replies.

phudgens

Technical User
Jul 8, 2004
117
US
I'm trying to use VBA to replace newline characters in a text file with any ascii character. The text file was saved as txt from a Word document, and the newline characters show up in the text file as little squares. I've tried the following:

Code:
    LineString = Replace(LineString, wdCRLF, "|")
    LineString = Replace(LineString, vbNewLine, "|")

These both compile and run, but do nothing. Does anyone know the solution?

Thanks,
Paul Hudgens
Denver
 


You must first examine the actual characters that trail a paragraph test. Use the Watch Window -- VERY useful!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
How are you opening and reading your data?

I saved a short two liner from word to a plain text file then used the following script.

Open "c:\TEMP\This-Test.txt" For Input As #1
Input #1, cBuffer

The Input statement stripped off the CRLF at the end of each record.

Switched to:

cBuffer = Input(36, #1)

cBuffer = Replace(cBuffer, Chr(10), "|") ' CR
cBuffer = Replace(cBuffer, Chr(13), "|") ' LF

This worked for the simple test.
 
Here's what I ended up doing:

Code:
Set fs = CreateObject("Scripting.FileSystemObject")
Set filA = fs.OpenTextFile(FileName, ForReading, TristateFalse)

Do While filA.AtEndOfStream <> True
    
    LineString = filA.ReadLine
    LineString = Replace(LineString, Chr(10), "|")
    LineString = Replace(LineString, Chr(13), "|")
    Call WordMaker(LineString, iWdCnt, Words, ExitSub, "|")
...
Loop

FileName is a file that was previously saved as a txt file from a Word document. Wordmaker is a function I wrote to parse the input string, in this case using the vertical bar as a delimiter. From there I'm able to search on specific strings, and then retrieve values from adjacent parsed words. The parts that I didn't have were Chr(10) and Chr(13) - those solved the problem, so thanks very much for the help.

Paul Hudgens


 
Depending on how WordMaker() handles double "|" sequences you may want to replace:

LineString = Replace(LineString, Chr(13), "|")

with

LineString = Replace(LineString, Chr(13), "")

Or drop the chr(10) replace and change the call to Wrokmaker to

Call WordMaker(LineString, iWdCnt, Words, ExitSub, chr(10))

Save a little processing time if important in your project.

Glad to help.
 
I considered that possibility, but so far it has not been a problem. One or the other is being replaced, but apparently not both. At some point I'll try one and then the other by themselves and see what I get. Thanks again.
 
Hear appears to be what is happening:

ReadLine() -- Reads an entire line (up to, but not including, the newline character) from a TextStream file and returns the resulting string.

A newline character would be chr(10). Your 'lineString' has a chr(13) at the end or each 'lineString'.

A decimal 10, chr(10), is a line feed or newline char.
A decimal 13, chr(13), is a carriage return char.

If you can find an old DOS utility called List.com it can display to you the hex of anything: exe, com, text, xls, etc

Got to go -- their turning out the lights... :)
 
Thanks for the info. This is all new and good information for me. Really appreciate the help. If they were already turning out the lights, you must not be in the US?
 
Actually in the midwest. Sometimes when you read to many web pages your eyes glaze over, things appear in the mirror dark that actual. :)

Trying to fingure out how to click a Sun Java button without knowing its element name or tag ID. After 4 days looking and post on this board, no luck. :(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top