Summary

This article shows you two procedures that use the EnumPrinters API function to retrieve a list of printers available locally and that your computer is connected to on the network.

The procedures demonstrate returning data to both the PRINTER_INFO_1 structure and the PRINTER_INFO_4 structure, each of which provides slightly different information about the printer. If you are using Microsoft Windows 95 or later, you can return data to the PRINTER_INFO_1 structure; if you are using Microsoft Windows NT, you can return data to either structure.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

More Information

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Use the Sub routine EnumeratePrinters1 if you are running Microsoft Windows 95 or later. You can use either Sub routine if you are running Microsoft Windows NT.

The code in each of the two examples performs these steps:

Calls the EnumPrinters API, which returns a buffer of data, a success flag, and a flag indicating whether the buffer is too small.

If the buffer is too small, makes it the correct size and calls the API again.

For each entry in the buffer, extracts the data and prints it.

NOTE: When using these functions in your own applications, you may want to return an array of data that can be displayed in a list box or grid rather than print the values.

The buffer is in the following format:

|rec1|rec2|...|recn|...garbage...|strn|...|str2|str1|

INFO records containing string pointers and numeric values are added from the left, and the string text for each record is added from the right. The area between the end of the last record and the start of the last string text contains garbage.

The buffer is declared as type Long, which makes extracting data from the records easier - all fields are either Long or pointers (pointers map to Long).

The StrLen() and PtrToStr() functions are used to set up the receiving string variable and to copy the text from the buffer to the variable.

Create a module and type the following lines in the Declarations section:

Option Explicit

Const PRINTER_ENUM_CONNECTIONS = &H4 Const PRINTER_ENUM_LOCAL = &H2

Type PRINTER_INFO_1 flags As Long pDescription As String PName As String PComment As String End Type

Type PRINTER_INFO_4 pPrinterName As String pServerName As String Attributes As Long End Type