Last active
June 21, 2019 15:57
-
-
Save imabug/3a3bbfdf9f2d3d20aada8826d59653dd to your computer and use it in GitHub Desktop.
Get a list of installed printers using VBA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'Needed to get a list of installed printers and their ports on a Windows 7 machine | |
'Based on code from https://docs.microsoft.com/en-us/office/vba/access/concepts/printing/retrieve-a-list-of-installed-printers | |
Option Compare Database | |
Sub ShowPrinters() | |
Dim strCount As String | |
Dim strMsg As String | |
Dim prtLoop As Printer | |
Dim fileNum As Integer | |
On Error GoTo ShowPrinters_Err | |
'Open a file to store the list of printers | |
fileNum = FreeFile | |
Open "printers.txt" For Output As #fileNum | |
If Printers.Count > 0 Then | |
'Get count of installed printers | |
strMsg = "Printers installed: " & Printers.Count & vbCrLf & vbCrLf | |
'Enumerate printer system properties | |
For Each prtLoop In Application.Printers | |
With prtLoop | |
'Spit the printer out to the file | |
Write #fileNum, "Device name: " & .DeviceName & vbCrLf _ | |
& "Driver name: " & .DriverName & vbCrLf _ | |
& "Port: " & .Port & vbCrLf & vbCrLf | |
End With | |
Next prtLoop | |
Close #fileNum | |
Else | |
strMsg = "No printers are installed" | |
End If | |
MsgBox Prompt:="Printer list written", Buttons:=vbOKOnly, Title:="Printer list" | |
ShowPrinters_End: | |
Exit Sub | |
ShowPrinters_Err: | |
MsgBox Prompt:=Err.Description, Buttons:=vbCritical & vbOKOnly, _ | |
Title:="Error Number " & Err.Number & " Occurred" | |
Resume ShowPrinters_End | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment