Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Visual Basic, WSH and Default Printers

Posted on 2003-03-25
Medium Priority
Last Modified: 2013-11-25

I am having a rather interesting (and frustrating) problem.

Due to the rather convoluted environment that my Visual Basic 6.0 application runs on, I need to use WSH to set destination printers (due to only default printers being available via the Printer object).

Now, because of this, I built a custom printer dialogue box and intended to do the following, so first off I created a small test application just to make sure the theory worked.  It was basically a one-form application with a single dropdown list and a single button.  The code for the form was:

Private Sub Form_Load()

Dim colPrinters As IWshCollection_Class
Dim myNetwork As IWshRuntimeLibrary.IWshNetwork_Class
Dim PrinterCounter As Long

Set myNetwork = New IWshRuntimeLibrary.IWshNetwork_Class

Set colPrinters = myNetwork.EnumPrinterConnections

For PrinterCounter = 0 To colPrinters.Count - 1 Step 2
    cmbPrinters.AddItem colPrinters(PrinterCounter + 1)
Next PrinterCounter

End Sub
Private Sub btnPrint_Click()

Dim myNetwork As IWshRuntimeLibrary.IWshNetwork_Class
Dim defaultPrinter As String

Set myNetwork = New IWshRuntimeLibrary.IWshNetwork_Class

defaultPrinter = Printer.DeviceName

myNetwork.SetDefaultPrinter cmbPrinters

MsgBox Printer.DeviceName

myNetwork.SetDefaultPrinter defaultPrinter

MsgBox Printer.DeviceName

End Sub

This works fine...when I run the application, the message boxes show that the printer.devicename changes with each SetDefaultPrinter.

So, full of confidence, I imported the form to the full application and set it up as a printer dialogue for a form (click Print and printer dialogue appears).  Suddenly, the code doesn't work and I can't figure out why (calling Printer.Devicename always returns the original default regardless).

The full application loads about 10 forms and the form in question is not the main form.  Am I missing something here?  Have I inadvertently locked the Printer object?

Any help greatly appreciated.
Question by:OzWolf
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 8207359
I found the problem...a Printer.Orientation = 1 call deep within the system was locking the object.

Accepted Solution

DigitalXtreme earned 0 total points
ID: 8207603
question PAQ'ed and points refunded.

CS Moderator

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question