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


Choosing a Printer

Posted on 2003-03-28
Medium Priority
Last Modified: 2008-02-20
Is there a way to automatically route a print job to a printer that you specify in your code? Rather than just printing a document to the default printer, how can I tell my application to print to a different printer on my LAN?

What I would really like to do is set the printer (let's call it PRINTER_1) before the following code executes. It is code to print PDF files:

Sub PrintAllAcrobatDocsInDir()
Dim AcroExchAVDoc As Object, AcroExchPDDoc As Object, AcroExchApp As Object
Dim strFileName As String, strPath As String
Dim iNumberOfPages As Integer

Set AcroExchApp = CreateObject("AcroExch.App")
Set AcroExchAVDoc = CreateObject("AcroExch.AVDoc")
Set AcroExchPDDoc = CreateObject("AcroExch.PDDoc")

' Show the Acrobat Exchange window
' Set the directory / folder to use
' Get the first pdf file in the directory
strFileName = Dir(strPath + PDF_WILDCARD, vbNormal)
' Start the loop.
Do While strFileName <> ""

' Open the [strFileName] pdf file
AcroExchAVDoc.Open strPath + strFileName, ""
' Get the PDDoc associated with the open AVDoc
Set AcroExchPDDoc = AcroExchAVDoc.GetPDDoc
' Get the number of pages for this pdf [and subtract one as zero based]
iNumberOfPages = AcroExchPDDoc.GetNumPages - 1
' Print all pages in this document
AcroExchAVDoc.PrintPages 0, iNumberOfPages, POSTSCRIPT_LEVEL, True, False
' Close this file
AcroExchAVDoc.Close True
' Get the name of the next file in the directory
strFileName = Dir

' Close Acrobat Exchange
End Sub
Question by:mack1er
LVL 44

Expert Comment

ID: 8225005
you an make use of the CommonDialog Control, and then use the ShowPrinter method, to allow the user to select the desired printer.  Be aware, however, that the REAL effect of this is to CHANGE the currently selecxted DEFAULT printer to the newly selected printer, and then continue to print to the 'default' printer.  That means that after the program terminates, the Systems default printer has been changed to whatever printer the user selected.  

And there is no easy way to modify this behavior.  The DeviceName property of the printer object is READ-ONLY, so you cannot set the printer Back to the previous DEFAULT device.

rthis is from the VB Help system entry about the ShowPrinter method of the CommonDialog Control:

"Note   If the PrinterDefault property is set to True, you can print to the Visual Basic Printer object. In addition, when the PrinterDefault property is True, any changes the user makes in the Setup portion of the Print dialog box are used to change the printer settings in the user's Printer setup."



Expert Comment

ID: 8225034
Hello mack1er.

If you are forced to print with:

AcroExchAVDoc.PrintPages 0, iNumberOfPages, POSTSCRIPT_LEVEL, True, False

then the code of the class library AcroExch.AVDoc
has to be recompilable - what means you are the owner of that library and can change the code.

Normally third party components deliver a DEVMODE-Type and variable to set it before printing. If so i can deliver you the code for setting up a custom printer's devmode and printing with it.

If you are printing yourself with the printer object then the task is very easy:

Dim myPrinter as Printer
Set myPrinter = Printers(x)


Expert Comment

ID: 8226372
To print a file (or text), and be able to select the printer, use the code below.

Add a CommonDialog component and a RichTextBox component to your project.

To print to the default printer, simply comment out the section that selects the printer (i.e., the "With CommonDialog1" section).

Private Sub Command1_Click()
    On Error Resume Next
    RichTextBox1.FileName = "c:\zip\junk.txt"   ' or set the .Text property to some string value
    With CommonDialog1
        .CancelError = True
        .Flags = cdlPDReturnDC + cdlPDHidePrintToFile + cdlPDNoPageNums + cdlPDNoSelection + cdlPDNoWarning
    End With
    If (Err.Number <> cdlCancel) Then
        RichTextBox1.SelPrint (Printer.hDC)
    End If
End Sub

You can alter the printing flags for what you want to use.  These are supported:
cdlPDAllPages : Returns or sets the state of the All Pages option.

cdlPDCollate : Returns or sets the state of the Collate check box.

cdlPDDisablePrintToFile : Disables the Print To File check box.

cdlPDHelpButton : Causes the dialog box to display the Help button.

cdlPDHidePrintToFile : Hides the Print To File check box.

cdlPDNoPageNums : Disables the Pages option button and the associated edit control.

cdlPDNoSelection : Disables the Selection option button.

cdlPDNoWarning : Prevents a warning message from being displayed when there is no default printer.

cdlPDPageNums : Returns or sets the state of the Pages option button.

cdlPDPrintSetup : Causes the system to display the Print Setup dialog box rather than the Print dialog box.

cdlPDPrintToFile : Returns or sets the state of the Print To File check box.

cdlPDReturnDC : Returns a device context for the printer selection made in the dialog box. The device context is returned in the dialog box's hDC property.

cdlPDReturnDefault : Returns default printer name.

cdlPDReturnIC : Returns an information context for the printer selection made in the dialog box. An information context provides a fast way to get information about the device without creating a device context. The information context is returned in the dialog box's hDC property.

cdlPDSelection : Returns or sets the state of the Selection option button. If neither cdlPDPageNums nor cdlPDSelection is specified, the All option button is in the selected state.

cdlPDUseDevModeCopies : If a printer driver doesn't support multiple copies, setting this flag disables the copies edit control. If a driver does support multiple copies, setting this flag indicates that the dialog box stores the requested number of copies in the Copies property.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Accepted Solution

QJohnson earned 320 total points
ID: 8227659
You must know the DeviceName of that printer or you wouldn't be asking the question.  Perhaps the user got it from a combobox filled with code like

Dim prTest as Printer
For each prTest in Printers
  cboPrinters.AddItem prTest.DeviceName

or maybe you just hard code it, read it from an ini file or the registry, or whatever.

So - to set THAT printer as default and THEN SET IT BACK the way you found it:


Dim strCurrentPrinter as String
Dim prTest as Printer

   strCurrentPrinter = Printer.DeviceName

   For Each PrTest In Printers
      If PrTest.DeviceName = cboPrinters.Text Then
         Set Printer = PrTest
         Exit For
      End If

'       Of course, if you have a procedure to handle this, you would probably use the DeviceName
'       as an argument and that key test line would look more like:

'      If PrTest.DeviceName = pstrPrinterName Then

'        Now you have your default printer set where you want it.  
'        Now you do your printing.  

     Call MySubThatDoesPrinting

'         Then you set it back the way you found it with:

   For Each PrTest In Printers
      If PrTest.DeviceName = strCurrentPrinter Then
         Set Printer = PrTest
         Exit For
      End If

   set prTest = nothing


Expert Comment

ID: 10820073
This code doesn't work.  The default printer for Acrobat Exchange is set when it is launched.  This code resets the application printer, in my case the MSAccess program, not the Exchange program.  From all my research I have come to the conclusion there is NO WAY to set the printer for the Exchange Adobe product.

Expert Comment

ID: 10823476

   There is clearly some information missing that we need.  The Acrobat Exchange product can not have shipped from the vendor knowing what printers will be available in your environment, so it is not possible that it knows which one to set at launch-time.  So it must be configurable in some manner.  We just need to figure out what that manner is.

   There may have been a wizard that ran on first launch (or perhaps as part of the application's installation) that asked what printer to use.  Having gotten a choice from the user, the application must save it somewhere for later use.  And there has to be a way for the user to change it.  

   There is a printer setup dialog in the app's user interface.  And it appears that you can call that dialog with Adobe's ocx if your program isn't intended to run without user assistance.  For example, in the question original posed here, the developer printed using the .PrintPages method.  If the .PrintWithDialog method was used instead, the user could set the printer of their choice with Adobe's dialog.  At least this looks likely to me viewing the method names in the ocx that I have with Adobe Reader 6.0 (C:\Program Files\Adobe\Acrobat 6.0\Reader\ActiveX\pdf.ocx which I have never used).  I don't think I have the "Acrobat Exchange" program the question poser is using, so there may be methods available to him of which I am unaware.

  If you have to do the printer setting yourself (without user assistance), it would be wonderful if there were a simple property setting available for you.  If the poser has paid good money for this "AdobeExchange" developer library, I would very much expect that this would be a basic feature.  Failing that, I think you'll just have to figure out where and how Adobe stores that information for its own use.  I would have expected a registry setting, but haven't found anything useful in either CURRENT_USER\Software\Adobe or LOCAL_MACHINE\Software\Adobe.  Nor could I find anything resembling an Adobe .ini file.  So I can't be much help with this idea.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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 Month13 days, 2 hours left to enroll

579 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