Excel VBA Does Printer Exist

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Excel VBA Does Printer Exist

Post by MSingh »

Hi,

I've seen the code "Determing default printer infromation" in J Walkenbach's book.

How do i check if a pc has a printer or not?
If not, gives Msg exit sub
If yes, then do something
end sub

My simple attempt:

Sub Check_For_Printer()

Select Case Application.Activeprinter

Case 1, "" : MsgBox "No Active Printer, Check Printer Connection & Retry!", vbCritical, "No printer"

Case 2, Is <> "": do something

End Select

End Sub

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Excel VBA Does Printer Exist

Post by HansV »

I can't test whether the following version actually works - I don't have a PC without a default printer...

Code: Select all

Sub Check_For_Printer()
  If Application.ActivePrinter = "" Then
    MsgBox "No Active Printer, Check Printer Connection & Retry!", vbCritical, "No printer"
  Else
    ' Do something
    ...
  End If
End Sub
Keep in mind that the user can install a printer driver even if the printer is not physically present, so ActivePrinter doesn't tell you whether a printer is actually connected to the PC, only whether a printer driver has been installed and set as default.
Best wishes,
Hans