See:
Enumerating Local and Network Printers
http://www.mvps.org/vbnet/
Main Topics
Browse All TopicsI am trying to trap an error in some code that crashes if the suer does not have a printer installed. I have used the on error function to capture it, but if i then unload the form i get another error say it canot unload this context. So instead I want to check if there is a printer installed, if there is then this form can continue to run, otherwise display a message box with only the vbOk button telling of the error and then returning back to the main form.
ie click Report goes to report form and say is there a printer installed yes / no
if yes continue with execution and display the report
else
display message that a printer needs to be installed and wait for ok to be clicked
ok clicked return to main form and allow normall operation again.
if Report is clicked again the same happens.
I hope i have explained what i am trying to achive
thanks in advance.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
See:
Enumerating Local and Network Printers
http://www.mvps.org/vbnet/
Use this code
Public Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Public Function PrintersExist() As Boolean
On Error GoTo err_hand
Dim strDef As String
Dim strDi As String
strDef = Space(128)
strDi = GetProfileString("WINDOWS"
If strDi = "0" Then
PrintersExist = False
Else
PrintersExist = True
End If
Exit Function
err_hand:
PrintersExist = False
Exit Function
End Function
Business Accounts
Answer for Membership
by: TimCotteePosted on 2003-09-29 at 04:34:04ID: 9450533
Hi darkrain,
LOCAL, "", 1, longbuffer(0), numbytes, numneeded, numprinters) LOCAL, "", 1, longbuffer(0), numbytes, numneeded, numprinters) * c + 1))) ription, longbuffer(4 * c + 1)) * c + 2))) , longbuffer(4 * c + 2)) * c + 3))) ent, longbuffer(4 * c + 3))
This example uses some api calls to enumerate locally installed printers. You could modify this to simply return a count of the printers installed and use the existing If statement that checks for more than one to show your message box error.
' Get information about all of the local printers using structure 1. Note how
' the elements of the array are loaded into an array of data structures manually. Also
' note how the following special declares must be used to allow numeric string pointers
' to be used in place of strings:
Private Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Private Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Long) As Long
Private Declare Function EnumPrinters Lib "winspool.drv" Alias "EnumPrintersA" (ByVal flags As Long, ByVal name As String, ByVal Level As Long, pPrinterEnum As Long, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
Const PRINTER_ENUM_LOCAL = &H2
Private Type PRINTER_INFO_1
flags As Long
pDescription As String
pName As String
pComment As String
End Type
Private Sub Form_Load()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim longbuffer() As Long ' resizable array receives information from the function
Dim printinfo() As PRINTER_INFO_1 ' values inside longbuffer() will be put into here
Dim numbytes As Long ' size in bytes of longbuffer()
Dim numneeded As Long ' receives number of bytes necessary if longbuffer() is too small
Dim numprinters As Long ' receives number of printers found
Dim c As Integer, retval As Long ' counter variable & return value
Me.AutoRedraw = True 'Set current graphic mode to persistent
' Get information about the local printers
numbytes = 3076 ' should be sufficiently big, but it may not be
ReDim longbuffer(0 To numbytes / 4) As Long ' resize array -- note how 1 Long = 4 bytes
retval = EnumPrinters(PRINTER_ENUM_
If retval = 0 Then ' try enlarging longbuffer() to receive all necessary information
numbytes = numneeded
ReDim longbuffer(0 To numbytes / 4) As Long ' make it large enough
retval = EnumPrinters(PRINTER_ENUM_
If retval = 0 Then ' failed again!
Debug.Print "Could not successfully enumerate the printes."
End ' abort program
End If
End If
' Convert longbuffer() data into printinfo()
If numprinters <> 0 Then ReDim printinfo(0 To numprinters - 1) As PRINTER_INFO_1 ' room for each printer
For c = 0 To numprinters - 1 ' loop, putting each set of information into each element
' longbuffer(4 * c) = .flags, longbuffer(4 * c + 1) = .pDescription, etc.
' For each string, the string is first buffered to provide enough room, and then the string is copied.
printinfo(c).flags = longbuffer(4 * c)
printinfo(c).pDescription = Space(lstrlen(longbuffer(4
retval = lstrcpy(printinfo(c).pDesc
printinfo(c).pName = Space(lstrlen(longbuffer(4
retval = lstrcpy(printinfo(c).pName
printinfo(c).pComment = Space(lstrlen(longbuffer(4
retval = lstrcpy(printinfo(c).pComm
Next c
' Display name of each printer
For c = 0 To numprinters - 1
Me.Print "Name of printer"; c + 1; " is: "; printinfo(c).pName
Next c
End Sub
Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com