vbscript or program to print part of a text file

I have several thousand .txt files that I would like to print only part of.  I would like to print the file only up to the word "[COMMENTS]".  I would like to be able to select the file from a list, and then just by clicking the selected file, I would like the print program to print the file up to "[COMMENTS]" without opening it in notepad.  Currently, we have to open each file, then cut and paste the part we need and then print it.  I found a vbscript command  "notepad.exe /p " & strFilePath that will print the whole file, but how can I get it to stop at "[COMMENTS]"?  Is it possible to do in visual basic?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Const ForReading = 1
Dim fso, folder, fileList, textStream
Dim theFolderPath as string
Dim theData as string
theFolderPath = "D:\textfiles"
'create fso
Set fso = CreateObject("scripting.filesystemobject")
'get the folder
Set folder = fso.GetFolder(theFolderPath)
'get the Files collection
Set fileList = folder.Files
'show me
For Each f In fileList
    'open as text stream
    Set textStream = fso.openTextFile("d:\afile.txt", ForReading)
    'read all
    theData = textStream.readAll()
    theData = Mid(theData, 1, InStr(theData, "[COMMENTS]") - 1)
    Printer.Print f.Name & ":" & vbCrLf & theData

If you paste that into a Form_Load in VB() Or a button click event) it will process the folder indicated by "thefolderPath" and print the files for you.

Locate a file list box on (File1) your form and copy the code below. It will print to the default printer:

Private Sub File1_Click()
Dim ff As Integer, fileTxt As String, fileSplit() As String
ff = FreeFile
Open File1.Path & File1.FileName For Binary As #ff
fileTxt = Space(LOF(ff))
Get #ff, , fileTxt
Close #ff
fileSplit = Split(fileTxt, "[COMMENTS]", -1)
If UBound(fileSplit) > 0 Then
   'here you can define Printer specifications, such as orientation etc...
    Printer.Print fileSplit(0)
End If
End Sub

Private Sub Form_Load()
File1.Path = "c:\your folder path\"
End Sub

A very basic print loop uising the default printer:

Private Sub Command1_Click()

    Dim ff As Long
    Dim sLine As String
    Dim filename As String
    'prevent reentrance
    Command1.Enabled = False
    'specify a file or set from list
    filename = "c:\test.txt"
    ff = FreeFile()
    Open filename For Input As #ff
    Do While Not EOF(ff)
        Line Input #ff, sLine
        If InStr(1, sLine, "[COMMENTS]") Then  'done
            Exit Do
        End If
        Printer.Print sLine
    Close #ff
    Command1.Enabled = True
End Sub

Also see this:
Printer Object - A Primer

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eric68Author Commented:
Thank you very much for all your help!  It works perfectly!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.