Displaying attached Printers in a List Box

  • Thread starter biganthony via AccessMonster.com
  • Start date
B

biganthony via AccessMonster.com

Hello,

I have a list box on a form that displays the printers attached to the
computer. The lst box is called lstPrinters and I use the following code in
the form's Open event to populate the list box:

For Each prt In Application.Printers
Me!lstPrinters.AddItem prt.DeviceName
Next

This works well, except when I open the form on my laptop when the laptop is
not connected to the work network and the printers are not valid. All the
printers on my laptop are network printers used at work (using Novell iPrint).


Is there a way to add a timer that, say, after 30 seconds, the loop times out?
At the moment, the form is hanging because I have not logged onto a network
and therefore the attached network printers are no longer available. I was
thinking that maybe there could be a counter but I am not sure whether it
goes within the For-Loop. I put it outside the For-Loop and the form still
hanged. At the moment, there are about 10 printers installed in Windows but
none of them are attached either directly or through a network as I am using
local workstation connected to an open wirless network.

I hope this makes sense.

Thanks,
Anthony.
 
C

Clifford Bass

Hi Anthony,

You might try something like this:

Dim fs As New FileSystemObject

If fs.FolderExists("\\printserver\c$") Then
For Each prt In Application.Printers
Me!lstPrinters.AddItem prt.DeviceName
Next
End If
Set fs = Nothing

This will require the Microsoft Scripting Runtime reference added in
the VBA Editor (Tools menu, References). It works with a Windows Server. It
might work with a Novell server. Or you could check for a specific network
drive that you always map when connected.

Clifford Bass
 
D

Douglas J. Steele

Actually, you can do it without adding a reference to Microsoft Scripting
Runtime.

Dim fs As Object

Set fs = CreateObject("Scripting.FileSystemObject")

If fs.FolderExists("\\printserver\c$") Then
For Each prt In Application.Printers
Me!lstPrinters.AddItem prt.DeviceName
Next
End If

Set fs = Nothing
 
C

Clifford Bass

Hi Douglas,

True, true, true! I forgot about that as I usually like to see the
methods and properties pop up when I type the period after the object.
Thanks for the reminder!

Clifford Bass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top