?
Solved

vbscript or program to print part of a text file

Posted on 2005-03-03
5
Medium Priority
?
189 Views
Last Modified: 2008-02-01
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?

Thanks,
Eric
0
Comment
Question by:eric68
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 8

Assisted Solution

by:fatalXception
fatalXception earned 100 total points
ID: 13452682
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
    Printer.NewPage
next

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.

HTH
0
 
LVL 19

Assisted Solution

by:Shauli
Shauli earned 100 total points
ID: 13452699
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

S
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 800 total points
ID: 13452723
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
        DoEvents
    Loop
    Printer.EndDoc
    Close #ff
    Command1.Enabled = True
   
End Sub


Also see this:
Printer Object - A Primer
http://www.freevbcode.com/ShowCode.asp?ID=3646
0
 

Author Comment

by:eric68
ID: 13458987
Thank you very much for all your help!  It works perfectly!
0
 
LVL 8

Expert Comment

by:fatalXception
ID: 13462685
Excellent.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 8 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question