View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
GS[_2_] GS[_2_] is offline
external usenet poster
 
Posts: 3,514
Default Extract paragraphs from text file

"GS" wrote:

Const sProfaneWords$ = "word1,word2,word3" '//and so on

Dim vWord, vData, n&
' This assumes the full path and filename is held in 'sFilename'
vData = Split(ReadTextFile(sFilename), vbCrLf)

For Each vWord In Split(sProfaneWords, ",")
For n = LBound(vData) to UBound(vData)
If (InStr(vData(n), vWord) 0) _
Or (InStr(vData(n), "*") 0) Then
vData(n) = "~": Exit For
Next 'n ' ** compile error here
***
Next 'vWord
vData = Filter(vData, "~", False)

WriteTextFile Join(vData, vbCrLf), sFilename


Hi Gary. I tested this code, but I got a compile error that
said "Next without For".


Sorry, I did forget the 'End If'.

I figured it might be missing an "End If" above that Next n line,
which I was right (, I hope), and the code did produce output,
but.....this was not exactly the desired output. Your code
was only deleting single sentences or lines that contained a
profanity word.

For my purposes, a "paragraph" constitutes the ENTIRE
block of words that occurs between a pair of "*" border
lines. (The only exception is the first paragraph, because
there is no "*" border at the top of the input file)

Is it possible there was another bug in the code that I
missed?


If you use the 2nd suggestion it will remove all empty lines and
asterisk lines before searching for profane words. It's a 2-step
process but is more efficient than my 1st offering since it treats all
3 aspects of 'cleaning' up your files...

'Filter junk lines
For n = LBound(vData) to UBound(vData)
'I revised the next line to use Len()
If Len(vData(n)) = 0 Or InStr(vData(n), "*") 0 Then
vData(n) = "~": Exit For
End If
Next 'n

'Filter profane words
For Each vWord In Split(sProfaneWords, ",")
For n = LBound(vData) to UBound(vData)
If (InStr(vData(n), vWord) 0) Then
vData(n) = "~": Exit For
End If
Next 'n
Next 'vWord
vData = Filter(vData, "~", False)


You could post a download link to a sample file for testing if you
want!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion