?
Solved

Network Printer Control

Posted on 2005-02-27
10
Medium Priority
?
528 Views
Last Modified: 2012-06-27
How can I tell if a network printer is initializing?  
Is there a way to send a file to a printer and then explicitly change the printer and print to another?
0
Comment
Question by:nebbles
[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
  • 4
10 Comments
 
LVL 7

Accepted Solution

by:
Smallint earned 1600 total points
ID: 13418136
Give it a shot.

This will show you all printers and its status.

>>Is there a way to send a file to a printer and then explicitly change the printer and print to another?

How are you printing?


Cheers


Private Declare Function OpenPrinter Lib "winspool.drv" _
   Alias "OpenPrinterA" _
   (ByVal pPrinterName As String, _
   phPrinter As Long, _
   pDefault As PRINTER_DEFAULTS) _
   As Long

Private Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" _
   (ByVal hPrinter As Long, _
   ByVal Level As Long, _
   pPrinter As Byte, _
   ByVal cbBuf As Long, _
   pcbNeeded As Long) _
   As Long

Private Declare Function ClosePrinter Lib "winspool.drv" _
   (ByVal hPrinter As Long) _
   As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
   (Destination As Any, _
   Source As Any, _
   ByVal Length As Long)
   
Private Type PRINTER_DEFAULTS
   pDatatype As String
   pDevMode As Long
   DesiredAccess As Long
End Type

Private Type PRINTER_INFO_2
   pServerName As Long
   pPrinterName As Long
   pShareName As Long
   pPortName As Long
   pDriverName As Long
   pComment As Long
   pLocation As Long
   pDevMode As Long
   pSepFile As Long
   pPrintProcessor As Long
   pDatatype As Long
   pParameters As Long
   pSecurityDescriptor As Long
   Attributes As Long
   Priority As Long
   DefaultPriority As Long
   StartTime As Long
   UntilTime As Long
   Status As Long
   cJobs As Long
   AveragePPM As Long
End Type

Private Const ERROR_INSUFFICIENT_BUFFER = 122

Private Const PRINTER_STATUS_BUSY = &H200
Private Const PRINTER_STATUS_DOOR_OPEN = &H400000
Private Const PRINTER_STATUS_ERROR = &H2
Private Const PRINTER_STATUS_INITIALIZING = &H8000
Private Const PRINTER_STATUS_IO_ACTIVE = &H100
Private Const PRINTER_STATUS_MANUAL_FEED = &H20
Private Const PRINTER_STATUS_NO_TONER = &H40000
Private Const PRINTER_STATUS_NOT_AVAILABLE = &H1000
Private Const PRINTER_STATUS_OFFLINE = &H80
Private Const PRINTER_STATUS_OUT_OF_MEMORY = &H200000
Private Const PRINTER_STATUS_OUTPUT_BIN_FULL = &H800
Private Const PRINTER_STATUS_PAGE_PUNT = &H80000
Private Const PRINTER_STATUS_PAPER_JAM = &H8
Private Const PRINTER_STATUS_PAPER_OUT = &H10
Private Const PRINTER_STATUS_PAPER_PROBLEM = &H40
Private Const PRINTER_STATUS_PAUSED = &H1
Private Const PRINTER_STATUS_PENDING_DELETION = &H4
Private Const PRINTER_STATUS_PRINTING = &H400
Private Const PRINTER_STATUS_PROCESSING = &H4000
Private Const PRINTER_STATUS_TONER_LOW = &H20000
Private Const PRINTER_STATUS_USER_INTERVENTION = &H100000
Private Const PRINTER_STATUS_WAITING = &H2000
Private Const PRINTER_STATUS_WARMING_UP = &H10000


Private Sub GetPrintersStatus()

    Dim hPrinter As Long
    Dim ByteBuf As Long
    Dim BytesNeeded As Long
    Dim PI2 As PRINTER_INFO_2
    Dim PrinterInfo() As Byte
    Dim result As Long
    Dim strPrinterName As String
    Dim pDefaults As PRINTER_DEFAULTS
    Dim strStatus As String
    Dim prnPrinter
   
   
    For Each prnPrinter In Printers
   
        strPrinterName = prnPrinter.DeviceName

        'Set desired access security setting.
        pDefaults.DesiredAccess = PRINTER_ACCESS_USE
       
        'Call API to get a handle to the printer.
        result = OpenPrinter(strPrinterName, hPrinter, pDefaults)
        If result = 0 Then
           Exit Sub
        End If
       
        'Init BytesNeeded
        BytesNeeded = 0
       
        'Clear the error object of any errors.
        Err.Clear
       
        'Determine the buffer size that is needed to get printer info.
        result = GetPrinter(hPrinter, 2, 0&, 0&, BytesNeeded)
       
        If Err.LastDllError <> ERROR_INSUFFICIENT_BUFFER Then
           ClosePrinter hPrinter
           Exit Sub
        End If
       
        ReDim PrinterInfo(1 To BytesNeeded)
       
        ByteBuf = BytesNeeded
       
        'Obtenemos el status de la impresora
        result = GetPrinter(hPrinter, 2, PrinterInfo(1), ByteBuf, _
          BytesNeeded)
           
        If result = 0 Then
           ClosePrinter hPrinter
           Exit Sub
        End If
       
        CopyMemory PI2, PrinterInfo(1), Len(PI2)
       
        Select Case PI2.Status
       
            Case PRINTER_STATUS_BUSY
                strStatus = "PRINTER_STATUS_BUSY"
            Case PRINTER_STATUS_DOOR_OPEN
                strStatus = "PRINTER_STATUS_DOOR_OPEN"
            Case PRINTER_STATUS_ERROR
                strStatus = "PRINTER_STATUS_ERROR"
            Case PRINTER_STATUS_INITIALIZING
                strStatus = "PRINTER_STATUS_INITIALIZING"
            Case PRINTER_STATUS_IO_ACTIVE
                strStatus = "PRINTER_STATUS_IO_ACTIVE"
            Case PRINTER_STATUS_MANUAL_FEED
                strStatus = "PRINTER_STATUS_MANUAL_FEED"
            Case PRINTER_STATUS_NO_TONER
                strStatus = "PRINTER_STATUS_NO_TONER"
            Case PRINTER_STATUS_NOT_AVAILABLE
                strStatus = "PRINTER_STATUS_NOT_AVAILABLE"
            Case PRINTER_STATUS_OFFLINE
                strStatus = "PRINTER_STATUS_OFFLINE"
            Case PRINTER_STATUS_OUT_OF_MEMORY
                strStatus = "PRINTER_STATUS_OUT_OF_MEMORY"
            Case PRINTER_STATUS_OUTPUT_BIN_FULL
                strStatus = "PRINTER_STATUS_OUTPUT_BIN_FULL"
            Case PRINTER_STATUS_PAGE_PUNT
                strStatus = "PRINTER_STATUS_PAGE_PUNT"
            Case PRINTER_STATUS_PAPER_JAM
                strStatus = "PRINTER_STATUS_PAPER_JAM"
            Case PRINTER_STATUS_PAPER_OUT
                strStatus = "PRINTER_STATUS_PAPER_OUT"
            Case PRINTER_STATUS_PAPER_PROBLEM
                strStatus = "PRINTER_STATUS_PAPER_PROBLEM"
            Case PRINTER_STATUS_PAUSED
                strStatus = "PRINTER_STATUS_PAUSED"
            Case PRINTER_STATUS_PENDING_DELETION
                strStatus = "PRINTER_STATUS_PENDING_DELETION"
            Case PRINTER_STATUS_PRINTING
                strStatus = "PRINTER_STATUS_PRINTING"
            Case PRINTER_STATUS_PROCESSING
                strStatus = "PRINTER_STATUS_PROCESSING"
            Case PRINTER_STATUS_TONER_LOW
                strStatus = "PRINTER_STATUS_TONER_LOW"
            Case PRINTER_STATUS_USER_INTERVENTION
                strStatus = "PRINTER_STATUS_USER_INTERVENTION"
            Case PRINTER_STATUS_WAITING
                strStatus = "PRINTER_STATUS_WAITING"
            Case PRINTER_STATUS_WARMING_UP
                strStatus = "PRINTER_STATUS_WARMING_UP"
            Case 0
                strStatus = "PRINTER READY"
       
        End Select
       
        MsgBox "Printer: " & strPrinterName & " Status:" & strStatus
       
        'Close the printer handle.
        ClosePrinter hPrinter
       
    Next
   
   
End Sub


Private Sub Form_Load()
GetPrintersStatus
End Sub
0
 

Author Comment

by:nebbles
ID: 13443144
>>>Is there a way to send a file to a printer and then explicitly change the printer and print to another?
> How are you printing?
I open a HTML document in IE using:
   Set objIE = New SHDocVw.InternetExplorer
   objIE.navigate (MyHTMLPage.html)
   objIE.ExecWB SHDocVw.OLECMDID_PRINT, SHDocVw.OLECMDEXECOPT_DONTPROMPTUSER, 0, 0

0
 
LVL 7

Expert Comment

by:Smallint
ID: 13445186
To change default printer you can use something like that:

Cheers


    Dim OrigPrinter As String
    Dim SelectedPrinter As String
    Dim X As Printer
     
    SelectedPrinter = "HP LaserJet 1100"
    OrigPrinter = Printer.DeviceName
     
    'Set the printer to the selected printer
    For Each X In Printers
        If X.DeviceName = SelectedPrinter Then
            Set Printer = X
            Exit For
        End If
    Next
     
     'YOUR CODE TO PRINT TO THE PRINTER GOES HERE
   
     
    'Set the printer back to the default printer
    For Each X In Printers
        If X.DeviceName = OrigPrinter Then
            Set Printer = X
            Exit For
        End If
    Next

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:nebbles
ID: 13445505
Your original post does meet my needs.  It is a little more than I needed but I love complete answers.  

I have tried the above to change the printer and it actually does change the printer.  Unfortunally IE will print to the same printer regardless unless it is given enough time.  I currently have a very ugly but effective sleep command.  If you have a solution for this I will repost since it is another question.

Thanks.
0
 
LVL 7

Expert Comment

by:Smallint
ID: 13445609
Thanks to you.

Please, can you elaborate your ugly sleepy problem? ;)
0
 

Author Comment

by:nebbles
ID: 13445644
Using an API call
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sleep 30000  ' Sleep for 30000 milliseconds Or 30 seconds

I called it ugly since there is no logic involved.  I just hope that 30 seconds is long enough.
0
 
LVL 7

Expert Comment

by:Smallint
ID: 13445664
btw, I left a comment in Spanish XD Sorry...

'Obtenemos el status de la impresora

=

'We get printer status

0
 
LVL 7

Expert Comment

by:Smallint
ID: 13445673
But... Why do you need to wait? So long...??
0
 

Author Comment

by:nebbles
ID: 13445817
I'll use PRINTER1 and PRINTER2 for this example.

I change the default printer to PRINTER1.
IE will print to PRINTER1.
--- DELAY ---
I change the default printer to PRINTER2
      If the delay was long enough then IE will print to PRINTER2  //What I want it to do
      If the delay was not long enough then IE will print to PRINTER1   //What I do not want it to do
I restore the printer default to its starting position.

The DELAY length was found by testing it.
With your code I am now able to replace the DELAY with a routine that monitors the status of the printer.  If I know PRINTER1 has finished, I also know that IE will match the change in the default printer.
0
 
LVL 7

Expert Comment

by:Smallint
ID: 13454134
mmmm is strange, i've never seen such behaviour, and of course without further information i don't know answer. Better post another question, you will have more chances to get an answer.

Cheers





0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 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