# Printers class
{: .no_toc }

A **Printers** object is a read-only collection of every printer installed on the system. It is exposed through the implicit **Printers** global --- there is no user-callable constructor --- and yields a [**Printer**](/tB/Packages/VB/Printer/index.md) for each device, keyed by its **DeviceName**. Use it to enumerate the installed devices, or to switch the active printer with `Set Printer = Printers(name)`.

```tb check_build
Dim p As Printer
For Each p In Printers
    Debug.Print p.DeviceName, p.DriverName, p.Port
Next

Set Printer = Printers("HP LaserJet")    ' make this the active printer
```

* TOC
{:toc}

## What the collection contains

Each entry is a [**Printer**](/tB/Packages/VB/Printer/index.md) bound to the corresponding device. These instances are **immutable** descriptors --- they are intended for identification and for handing to `Set Printer = …`, not for running a print job directly. Assigning any of the settings properties on one raises run-time error 383 (*Property is read-only*); calling [**EndDoc**](/tB/Packages/VB/Printer/index.md#enddoc), [**KillDoc**](/tB/Packages/VB/Printer/index.md#killdoc), [**NewPage**](/tB/Packages/VB/Printer/index.md#newpage), [**Print**](/tB/Packages/VB/Printer/index.md#print), or the other document-control methods raises run-time error 438 (*Object doesn't support this property or method*). [**TrackDefault**](/tB/Packages/VB/Printer/index.md#trackdefault) is always **False** on these instances.

A driver that advertises a single device over multiple ports produces one [**Printer**](/tB/Packages/VB/Printer/index.md) entry per port; only the first such entry is keyed by **DeviceName**, the rest are accessible only by numeric index.

## Live enumeration

The collection is **not cached**. Every call to [**Count**](#count), [**Item**](#item), or `For Each` re-reads the system's installed-printers list from the Windows registry's profile section and reconstructs a fresh batch of [**Printer**](/tB/Packages/VB/Printer/index.md) instances. A printer added or removed in **Settings → Printers** therefore appears the next time the collection is touched, with no need to refresh anything from code. The trade-off is that consecutive accesses are not cheap --- when enumerating, cache the result if many lookups are needed:

```tb check_build
Dim p As Variant
Dim snapshot As Variant : snapshot = Array()      ' or use a Collection
For Each p In Printers
    snapshot = Array(snapshot, p.DeviceName)      ' (illustrative)
Next
```

## Indexing

Numeric indexing is **0-based**, in contrast with most VB6 collections, which are 1-based. The first installed printer is `Printers(0)`; the last is `Printers(Printers.Count - 1)`. An index past the end, or a negative one, raises run-time error 9 (*Subscript out of range*).

String indexing looks up by **DeviceName** --- new in twinBASIC; VB6's **Printers** supports only numeric access. A name that is not present raises run-time error 5 (*Invalid procedure call or argument*), as a [**Collection**](/tB/Modules/Collection/Item.md) does for a missing key.

## Properties

### Count
{: .no_toc }

The number of installed printer entries. **Long**, read-only. Re-counted on every access --- see [Live enumeration](#live-enumeration).

Syntax: *object*.**Count**

### Item
{: .no_toc }

Returns the [**Printer**](/tB/Packages/VB/Printer/index.md) at the given index. **Default property** --- `Printers(0)` is shorthand for `Printers.Item(0)`.

Syntax: *object*.**Item**( *Index* ) **As Printer**

*Index*
: *required* A **Variant**. As a **Long**, the zero-based position in the collection (`0` to `Count - 1`); a value past the end, or a negative one, raises run-time error 9. As a **String**, the **DeviceName** of the printer; an unknown name raises run-time error 5.

## See Also

- [Printer](/tB/Packages/VB/Printer/index.md) -- the printer object itself, and the implicit **Printer** global.
