Results 1 to 6 of 6

Thread: Printing a txt file

  1. #1

    Thread Starter
    Fanatic Member joltremari's Avatar
    Join Date
    Sep 2000
    Location
    Mississippi
    Posts
    674

    Printing a txt file

    I need the simplest way possible to print an existing *.txt file

    Any Help?

    JO
    "I have not failed. I've just found 10,000 ways that won't work."
    'Thomas Edison'

    "If we knew what it was we were doing it wouldn't be called research, would it?"
    'Albert Einstein'

    VB6

  2. #2
    Matthew Gates
    Guest
    You can do it two ways. By opening the text file and placing the contents into a variable and printing the variable, or using the ShellExecute API function (probably takes longer) to print it.


    To do it through a variable:


    Code:
    Private Sub Command1_Click()
        Dim sText As String
        Open "MyFile.txt" For Input As #1
            sText = Input(LOF(1), 1)
        Close #1
        Printer.Print sText
        Printer.EndDoc
    End Sub
    Or you can do it by using the ShellExecute API function:


    Code:
    Private Declare Function ShellExecute _
    Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As _
    Long, ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long
    
    
    Private Sub PrintFile(sFile As String)
        ShellExecute Me.hwnd, "Print", sFile, _
        vbNullString, vbNullString, 1 'or 0 for hidden
    End Sub
    
    
    Private Sub Command1_Click()
        Call PrintFile("MyFile.txt")
    End Sub

  3. #3

    Thread Starter
    Fanatic Member joltremari's Avatar
    Join Date
    Sep 2000
    Location
    Mississippi
    Posts
    674
    Could I use this one in Form_Load event then close the form after printing?

    Private Sub Command1_Click()
    Dim sText As String
    Open "MyFile.txt" For Input As #1
    sText = Input(LOF(1), 1)
    Close #1
    Printer.Print sText
    Printer.EndDoc
    End Sub


    JO
    "I have not failed. I've just found 10,000 ways that won't work."
    'Thomas Edison'

    "If we knew what it was we were doing it wouldn't be called research, would it?"
    'Albert Einstein'

    VB6

  4. #4
    Lively Member
    Join Date
    Mar 2001
    Location
    Brighton, England
    Posts
    112
    '
    Dim iFile As Integer
    Dim cData As String
    '
    iFile = FreeFile
    '
    Open "C:\(YOURFILENAME).txt" For Input As #iFile
    cData = Input(LOF(iFile), 1)
    Close
    'Debug.Print , cData
    Printer.Print , cData
    Printer.EndDoc
    '

    try this it whacks the whole file into a string and then prints the string, pretty quick

    hope it helps

    john

  5. #5
    Matthew Gates
    Guest
    Originally posted by joltremari
    Could I use this one in Form_Load event then close the form after printing?

    Private Sub Command1_Click()
    Dim sText As String
    Open "MyFile.txt" For Input As #1
    sText = Input(LOF(1), 1)
    Close #1
    Printer.Print sText
    Printer.EndDoc
    End Sub


    JO

    Sure.


    Code:
    Private Sub Form_Load()
        
        Dim sText As String
        Open "MyFile.txt" For Input As #1
            sText = Input(LOF(1), 1)
        Close #1
        Printer.Print sText
        Printer.EndDoc
        DoEvents
        Unload Me
        Set Form1 = Nothing
        End
    
    End Sub

  6. #6

    Thread Starter
    Fanatic Member joltremari's Avatar
    Join Date
    Sep 2000
    Location
    Mississippi
    Posts
    674

    Perfect

    Thank you,

    JO
    "I have not failed. I've just found 10,000 ways that won't work."
    'Thomas Edison'

    "If we knew what it was we were doing it wouldn't be called research, would it?"
    'Albert Einstein'

    VB6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width