Visual Basic, WSH and Default Printers

G'day,

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.
OzWolfAsked:
Who is Participating?
 
DigitalXtremeConnect With a Mentor Commented:
question PAQ'ed and points refunded.

DigitalXtreme
CS Moderator
0
 
OzWolfAuthor Commented:
I found the problem...a Printer.Orientation = 1 call deep within the system was locking the object.
0
All Courses

From novice to tech pro — start learning today.