Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


vbscript or program to print part of a text file

Posted on 2005-03-03
Medium Priority
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?

Question by:eric68

Assisted Solution

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

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.

LVL 19

Assisted Solution

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

LVL 32

Accepted Solution

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
    Close #ff
    Command1.Enabled = True
End Sub

Also see this:
Printer Object - A Primer

Author Comment

ID: 13458987
Thank you very much for all your help!  It works perfectly!

Expert Comment

ID: 13462685

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

581 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