?
Solved

Choosing a Printer

Posted on 2003-03-28
6
Medium Priority
?
484 Views
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
AcroExchApp.Show
' Set the directory / folder to use
strPath = PDF_DIRECTORY
' 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
Loop

' Close Acrobat Exchange
AcroExchApp.Exit
End Sub
0
Comment
Question by:mack1er
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
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."

AW

0
 
LVL 6

Expert Comment

by:VK
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)
....

V.K.
0
 
LVL 9

Expert Comment

by:GivenRandy
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
        .ShowPrinter
    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.
---
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
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
Next

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
   Next

'       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
   Next

   set prTest = nothing

=========================================================
0
 

Expert Comment

by:johnhrschuster
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.
0
 
LVL 3

Expert Comment

by:QJohnson
ID: 10823476
John,

   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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

770 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