Printer Control in VB6

Posted on 2011-10-23
Last Modified: 2012-05-12
I have a VB6 program that uses straight printer. type commands to the default printer.
I now need to print a logo and the Dim text fields.  
How do i add a printer control so that the user can select the printer they want?
Should my logo/data shoot to the printer directly or go to a Form first? (I am not using a database)  Please show instruction, Thank You
Question by:GarySB
    LVL 44

    Assisted Solution

    by:Martin Liss
    Dim WhatYouWantToPrint As String
    Printer.Print WhatYouWantToPrint, CommonDialog1.hDC

    Open in new window

    LVL 2

    Expert Comment

    This isn't a runnable answer to your question. It's a couple of building blocks that you may find useful if you want more control of your UI and/or your app needs to have the ability to send different jobs, such as reports vs.receipts, to different printers.  

    In your Preferences dialog, the user picks the printer for the job from a combo (dropdown list). You can store the selection in a config file or similar for later.

    Dim i       As Integer
    With cboPrinters
        For i = 0 To Printers.Count - 1
            .AddItem Printers(i).DeviceName
    End With

    Open in new window

    Then assign the selection to a global variable:

    g_strPrinterName = cboPrinters.Text ' or read it from the config file

    Open in new window

    When you get ready to print:

    Dim prn	As Printer
    For Each prn In Printers
        If (prn.DeviceName = g_strPrinterName) Then
            Set Printer = prn
            Exit For
        End If

    Open in new window

    LVL 14

    Accepted Solution

    Here's how I do it (excuse my use of Implicit variable declaration):

       Rem *** Save default printer ***
       For i% = 0 To Printers.Count - 1
          If Printers(i%).DeviceName = Printer.DeviceName Then
             SavedPrn% = i%   'this variable is a public (global) variable
             Exit For
          End If
       Next i%

    ' Select your printer here. I have a dialog for this, or use the common dialog.

    ' Do your printing...

       Rem *** Restore default printer ***
       Set Printer = Printers(SavedPrn%)


    (note that "Alpha" is the name of my opening "start" form that has the logo on it)
    (this code allows me to call this routine from any other form)
    (my logo is contained in the pctPrint PictureBox control)

       Alpha.pctPrint.ScaleMode = vbPixels
       start% = (Printer.ScaleWidth / 2) - (Alpha.pctPrint.Width / 2)
       Call Printer.PaintPicture(Alpha.pctPrint.Picture, start%, Printer.CurrentY)
       Printer.CurrentY = Printer.CurrentY + Alpha.pctPrint.Height

    Author Closing Comment

    Both answers were exactly what I needed, Thanks

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    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…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    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

    14 Experts available now in Live!

    Get 1:1 Help Now