Results 1 to 21 of 21

Thread: printer offline

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    printer offline

    How to alert the user with message box that tell them that printer they want to use is offline? I want to print on paper only the printer is online.

    Code:
    Private Sub Command1_Click()
    Dim p As Printer
    For Each p In Printers
    If p.DeviceName = Combo1.Text Then Set Printer = p: Exit For
    Next
    Printer.Print "this is a test"  ' print something
    Printer.EndDoc   ' finish
    End Sub
    
    Private Sub Form_Load()
    'Add all the available printers to the combo box
    For i% = 0 To Printers.Count - 1
        Combo1.AddItem Printers(i%).DeviceName
    Next i%
    
    'Display the default printer
    For i% = 0 To Combo1.ListCount - 1
    If Combo1.List(i%) = Printer.DeviceName Then
    Combo1.ListIndex = i%
    Exit For
    End If
    Next i%
    End Sub

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: printer offline

    The method that would involve the least amount of code would be to error trap the printer routine.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: printer offline

    How to do error trapping to aleat the user the user that the printer of their choice as offline?

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: printer offline

    Step 1: Take the printer off line, run your code, and see what error is generated.

    Step 2: Take the error code that is generated and use it in your error trap.
    Code:
    Private Sub Command1_Click()
    On Error GoTo PrintError
    Dim p As Printer
    For Each p In Printers
    If p.DeviceName = Combo1.Text Then Set Printer = p: Exit For
    Next
    Printer.Print "this is a test"  ' print something
    Printer.EndDoc   ' finish
    Exit Sub
    PrintError:
    If Err.Number = xx Then
       Msgbox "The printer is off line.   Please try again."
    Else
       Msgbox "An error has occurred.   It is " & Err.Number & " " & Err.Description
    End If
    End Sub
    Where xx is error number that comes back when the printer is off line.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: printer offline

    BTW: The Else is for any error that might occur not related to the printer being off line.

  6. #6
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: printer offline

    Quote Originally Posted by Hack
    The method that would involve the least amount of code would be to error trap the printer routine.
    Yep.

    Matrik, check this http://support.microsoft.com/kb/202480
    see the lines of codes

    Some other info

    http://www.merrioncomputing.com/Prog...tchPrinter.htm

    IIF(Post.Rate > 0 , , )

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: printer offline

    Quote Originally Posted by zeezee
    Matrik, check this http://support.microsoft.com/kb/202480see the lines of codes
    Yeah, I'm familiar with that one which is why I always recommend just letting it blow up and trapping the error.

  8. #8
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: printer offline


    Its much better your way, rather than all those APIs Declarations, Types ,etc ...

    One question though, would it be the same Error No for all printers

    IIF(Post.Rate > 0 , , )

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: printer offline

    Quote Originally Posted by zeezee
    One question though, would it be the same Error No for all printers
    Theoretically....I suspect that actual error will have something to do with being unable to connect to it. I'm not sure if it would be able to tell the difference between the various ways of not being able to connect to it, so I suspect the error would be the same.

    If it isn't, then that should get covered with the Else statement in the error trap.
    Last edited by Hack; Jan 31st, 2008 at 07:53 AM.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: printer offline

    I have select the printer from combo box. When I select the printer, It not tell me that the printer is offline. I just pending. I also did not get error message so I did not get the error number. I not using printer common dialog

    Code:
    Dim p As Printer
    For Each p In Printers
    If p.DeviceName = Combo1.Text Then Set Printer = p: Exit For
    Next
    Printer.Print "this is a test"  ' print something
    Printer.EndDoc   ' finish

  11. #11
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: printer offline

    @ Hack
    Yep, you are right.
    Even that KB article says its depending on the printer/ Driver.
    Some status would depend on the Job que. And as it says, we cant directly querry the printer and tell its online or offline. So your method would work 100% since it captures the error either way.

    IIF(Post.Rate > 0 , , )

  12. #12
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: printer offline

    Matrik, as I said, the Online Offline would depend on many things, take a look at the description on that article.

    See the attached image. It shows the current printers in my PC. Only the Network printer shows unable to connect. All otheres are Ready. But actually only the Software Printers are there. I dont have any printer connected to my PC now. But it shows ready.

    So it would depend on the Driver and Job Que I guess.

    Attached Images Attached Images  
    IIF(Post.Rate > 0 , , )

  13. #13
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: printer offline

    Well, you could try the code in the link that zeezee posted. That does give a lot of information.

  14. #14
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: printer offline

    @ Matrik
    I tried the code you have posted here. But when I print it, it goes to the que and waits there for a while. The Error is raised only if the document is canceled from the que. it occurs in line

    Printer.Print "this is a test" ' print something

    So, when you want to capture the error, you may have to cancel the document manually or set some interval to cancel that.

    And that article's code, is giving same results in the image I posted. All printers are ready

    IIF(Post.Rate > 0 , , )

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: printer offline

    I got this sample from planetsourcecode but I don't know how to use and call it.
    Attached Files Attached Files

  16. #16
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: printer offline

    @Matrik02

    Since its a class, create an object of it and call the methods or get properties. Explanation may be there in original article.

    But that class is omewhat similar to the code in the article I gave. And that class doesnt have anything about printer status.

    IIF(Post.Rate > 0 , , )

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: printer offline

    the printer said ready when I on the printer, but when I switch off the printer power, it still said ready. I want to get the message box to alert the user the printer is ready, that mean the printer is in off from the power cable.

    what ready mean?

  18. #18
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: printer offline

    what ready mean?
    I think it says the Job que is ready to accept any jobs? May be trying to set spooling off would do something. Not sure.
    Is this printer going to be a specific one or could be any printer? If its only one kind of printer, you might be able to get it by using its driver. Or use its connected port to querry. But just a huntch.

    IIF(Post.Rate > 0 , , )

  19. #19
    New Member
    Join Date
    Jan 2008
    Posts
    6

    Re: printer offline

    There must be a way to get printer status directly from querying the port without going through the spooler. Any ideas?

    Magaero

  20. #20
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: printer offline

    try this
    see this example from Allapi.net:
    'Code generously provided by Merrion Computing
    'Visit their website at http://www.merrioncomputing.com/
    Private Const CCHDEVICENAME = 32
    Private Const CCHFORMNAME = 32
    Private Type DEVMODE
    dmDeviceName As String * CCHDEVICENAME
    dmSpecVersion As Integer
    dmDriverVersion As Integer
    dmSize As Integer
    dmDriverExtra As Integer
    dmFields As Long
    dmOrientation As Integer
    dmPaperSize As Integer
    dmPaperLength As Integer
    dmPaperWidth As Integer
    dmScale As Integer
    dmCopies As Integer
    dmDefaultSource As Integer
    dmPrintQuality As Integer
    dmColor As Integer
    dmDuplex As Integer
    dmYResolution As Integer
    dmTTOption As Integer
    dmCollate As Integer
    dmFormName As String * CCHFORMNAME
    dmUnusedPadding As Integer
    dmBitsPerPel As Integer
    dmPelsWidth As Long
    dmPelsHeight As Long
    dmDisplayFlags As Long
    dmDisplayFrequency As Long
    End Type
    Private Type PRINTER_INFO_2
    pServerName As String
    pPrinterName As String
    pShareName As String
    pPortName As String
    pDriverName As String
    pComment As String
    pLocation As String
    pDevMode As Long
    pSepFile As String
    pPrintProcessor As String
    pDatatype As String
    pParameters As String
    pSecurityDescriptor As Long
    Attributes As Long
    Priority As Long
    DefaultPriority As Long
    StartTime As Long
    UntilTime As Long
    Status As Long
    JobsCount As Long
    AveragePPM As Long
    End Type
    Private Type PRINTER_DEFAULTS
    pDatatype As String
    pDevMode As DEVMODE
    DesiredAccess As Long
    End Type
    Public Enum Printer_Status
    PRINTER_STATUS_READY = &H0
    PRINTER_STATUS_PAUSED = &H1
    PRINTER_STATUS_ERROR = &H2
    PRINTER_STATUS_PENDING_DELETION = &H4
    PRINTER_STATUS_PAPER_JAM = &H8
    PRINTER_STATUS_PAPER_OUT = &H10
    PRINTER_STATUS_MANUAL_FEED = &H20
    PRINTER_STATUS_PAPER_PROBLEM = &H40
    PRINTER_STATUS_OFFLINE = &H80
    PRINTER_STATUS_IO_ACTIVE = &H100
    PRINTER_STATUS_BUSY = &H200
    PRINTER_STATUS_PRINTING = &H400
    PRINTER_STATUS_OUTPUT_BIN_FULL = &H800
    PRINTER_STATUS_NOT_AVAILABLE = &H1000
    PRINTER_STATUS_WAITING = &H2000
    PRINTER_STATUS_PROCESSING = &H4000
    PRINTER_STATUS_INITIALIZING = &H8000
    PRINTER_STATUS_WARMING_UP = &H10000
    PRINTER_STATUS_TONER_LOW = &H20000
    PRINTER_STATUS_NO_TONER = &H40000
    PRINTER_STATUS_PAGE_PUNT = &H80000
    PRINTER_STATUS_USER_INTERVENTION = &H100000
    PRINTER_STATUS_OUT_OF_MEMORY = &H200000
    PRINTER_STATUS_DOOR_OPEN = &H400000
    PRINTER_STATUS_SERVER_UNKNOWN = &H800000
    PRINTER_STATUS_POWER_SAVE = &H1000000
    End Enum
    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, buffer As Long, ByVal pbSize As Long, pbSizeNeeded 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 Declare Function IsBadStringPtrByLong Lib "kernel32" Alias "IsBadStringPtrA" (ByVal lpsz As Long, ByVal ucchMax As Long) As Long
    Public Function StringFromPointer(lpString As Long, lMaxLength As Long) As String
    Dim sRet As String
    Dim lret As Long
    If lpString = 0 Then
    StringFromPointer = ""
    Exit Function
    End If
    If IsBadStringPtrByLong(lpString, lMaxLength) Then
    ' An error has occured - do not attempt to use this pointer
    StringFromPointer = ""
    Exit Function
    End If
    ' Pre-initialise the return string...
    sRet = Space$(lMaxLength)
    CopyMemory ByVal sRet, ByVal lpString, ByVal Len(sRet)
    If Err.LastDllError = 0 Then
    If InStr(sRet, Chr$(0)) > 0 Then
    sRet = Left$(sRet, InStr(sRet, Chr$(0)) - 1)
    End If
    End If
    StringFromPointer = sRet
    End Function
    Private Sub Form_Load()
    Dim SizeNeeded As Long, buffer() As Long
    Dim pDef As PRINTER_DEFAULTS
    'Get a handle to the printer
    lret = OpenPrinter(Printer.DeviceName, mhPrinter, pDef)
    'Initialize the buffer
    ReDim Preserve buffer(0 To 0) As Long
    'Retrieve the required size (in bytes)
    lret = GetPrinter(mhPrinter, 2, buffer(0), UBound(buffer), SizeNeeded)
    'Resize the buffer... Note that a Long is four bytes
    ReDim Preserve buffer(0 To (SizeNeeded / 4) + 3) As Long
    'Retrieve the Printer information
    lret = GetPrinter(mhPrinter, 2, buffer(0), UBound(buffer) * 4, SizeNeeded)
    'The data stored in 'buffer' corresponds with the data of a PRINTER_INFO_2 structure
    ClosePrinter mhPrinter
    'Show the data
    PrintData "Server name", StringFromPointer(buffer(0), 255)
    PrintData "Printer name", StringFromPointer(buffer(1), 255)
    PrintData "Share name", StringFromPointer(buffer(2), 255)
    PrintData "Port name", StringFromPointer(buffer(3), 255)
    PrintData "Driver name", StringFromPointer(buffer(4), 255)
    PrintData "Comment", StringFromPointer(buffer(5), 255)
    PrintData "Location", StringFromPointer(buffer(6), 255)
    Unload Me
    End Sub
    Sub PrintData(Name As String, Data As String)
    If LenB(Data) > 0 Then
    Debug.Print Name + ": " + Data
    End If
    End Sub

    * Add to Knowledgebase
    * View More Solutions
    *
    Bookmark:
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  21. #21
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: printer offline

    isnoend07, again, please always use Code (or VBCode) tags when posting code.

    If you do that, it is displayed in a more readable way, and keeps the indentation (which is very useful).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width