Posted on 2005-05-03
Last Modified: 2013-11-25
I need to know howto print from visual basic 6. I already know about the paintpicture method. I want to print a textfile.
Question by:tim102096
    LVL 28

    Assisted Solution

    You could use ShellExecute API:

    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 Command1_Click()
    Call ShellExecute(0, "Print", ("C:\Somefile.txt"), "", "", 0)
    End Sub
    LVL 12

    Assisted Solution

    Shamelessly copied from another question:

    See the microsoft KB article:

    HOWTO: Use ShellExecute to Launch Associated File (32-bit)

    In the example you will see the line:

       StartDoc = ShellExecute(Scr_hDC, "Open", DocName, _
              "", "C:\", SW_SHOWNORMAL)

    If you change the "Open" to "Print" in this line, it will print the document to the default printer using the native application that the file belongs to.  For example, a .DOC file is associated with Word.  This will print the document through word...

    By the way, if you dont want to have the application visible, you would change that line to:

       StartDoc = ShellExecute(Scr_hDC, "Print", DocName, _
              "", "C:\", 0)

    LVL 7

    Assisted Solution

    You mentioned the PaintPicture method; does that mean that you want to Print text, as in draw it on the screen?

    You can do this using a Picture Box control, similar to the PaintPicture method except with text. First load the text you want into a file:

    Dim strText as String
    Dim FileNo as Integer

    Open App.Path & "\Text.txt" for Input as #FileNo
        strText = Input(LOF(FileNo), #FileNo)
    Close #FileNo

    ...and then you can print it to a Picture Box:

    Picture1.CurrentX = 0
    Picture1.CurrentY = 0
    Picture1.Print strText

    You can also set the foreground colors, font, etc.

    LVL 38

    Assisted Solution

    You can print simple text as well as print pictures using the Printer object:

    Here's some simple code to dump a text file line by line to the printer

    Private Sub PrintTextFile(strPath As String)
        Dim hFile As Integer
        Dim strLine As String

        hFile = FreeFile
        Open strPath For Input As #hFile
        Do While Not EOF(hFile)
            Line Input #hFile, strLine
            If Printer.CurrentY + Printer.TextHeight(strLine) > Printer.ScaleHeight Then
            End If
            Printer.Print strLine
        Close #hFile
    End Sub
    LVL 1

    Accepted Solution

    Function PrintFile(FileName) 'filename is the name of the file with its path c:/autoexec.bat"
    open filename for input as #1
    open "lpt1" for output as #1 'printer port
    do while not eof(1)
    lineinput #1,xx
    print #2,xx
    close #1
    close #2
    end function

    Author Comment

    Thankyou. This is exactly what i wanted.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    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…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now