Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printer Names in Combo Box

Status
Not open for further replies.

SUnderwood

Programmer
Nov 21, 2002
107
GB

Hi All,

I'm having a great time trying to figure THIS one out!

Using VBA I want to put a list of all available printers into a combo box for my users to select from. I will then set the current/active printer to that selected and print to it.

Sounds easy? Yes, and so it should be, but NO! It’s a GHASTLY HORROR!!!! HELP!!!!!

Sean
 
Are you using Access or VB or what?
VB and acces have a Active -X called common dialog box which will present a list of printers or files or colrs depending on how you configure it.

DougP, MCP
 
So, after all this information gathering, what is the VBA code that will display the Printer Box? I want to paste it into my (for the most part) recorded VBA Macro.

Last time I wrote a macro the code was
CMDialog.Activate=4 - or something similar.
It seems not to work now and I, too, am desparate to display the box.

As an ex tecchie, as I see the wheel has been invented, I'm not keen to wade through all the stuff and re-invent it - call me idle if you will !
 
SUnderwood's question has a fairly simple answer which is as follows (which I'll post here from an API forum for the benefit of other VBA folks):

One solution that you may want to try, and it's not that difficult, but you do need to include a reference to the Windows Script Host Object Model in your code.

One you setup your combobox, as type ValueList, execute the following from within the form to populate the combobox.
Code:
   Dim lStr_PrinterList   As String
   
   lStr_PrinterList = ListAllPrinters
   cboPrinters.RowSource = lStr_PrinterList

And in a module, add the ListAllPrinters function. You skip every other one because the even numbered entries have the Port names, and you don't have to include those.
[code]
Public Function ListAllPrinters() As String

   Dim lObj_ScriptControl        As IWshNetwork_Class
   Dim lCol_Printers             As IWshCollection_Class
   Dim lStr_PrinterList          As String
   Dim lInt_Idx                  As Integer
   
   Set lObj_ScriptControl = New IWshNetwork_Class
   Set lCol_Printers = lObj_ScriptControl.EnumPrinterConnections

   lStr_PrinterList = vbNullString
   For lInt_Idx = 1 To lCol_Printers.Count - 1 Step 2
      lStr_PrinterList = lStr_PrinterList & lCol_Printers.Item(lInt_Idx) & ";"
   Next lInt_Idx

   If (Right(lStr_PrinterList, 1) = ";") Then
      lStr_PrinterList = Left(lStr_PrinterList, Len(lStr_PrinterList) - 1)
   End If

   Set lObj_ScriptControl = Nothing
   Set lCol_Printers = Nothing
   
   ListAllPrinters = lStr_PrinterList

End Function
Then in the AfterUpdate event of the combobox add the following:
Code:
Private Sub cboPrinters_AfterUpdate()
   SetPrinterAsDefault cboPrinters.Text
End Sub
And in the module, add the SetPrinterAsDefault function
Code:
Public Sub SetPrinterAsDefault(rStr_PrinterName As String)

   Dim lObj_ScriptControl        As IWshNetwork_Class
   
   Set lObj_ScriptControl = New IWshNetwork_Class
   lObj_ScriptControl.SetDefaultPrinter rStr_PrinterName
   Set lObj_ScriptControl = Nothing

End Sub

But I think your question, PinkF, is a completely different one, that being how to show the Printer Properties Dialog, and is that is correct, it's probably best that it be in it's own thread.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
That's a good question IvanMoala.

In some cases I would use that, if I could guarantee that my entire user community had a late enough versions of the various applications that support that functionality.

In some cases I would not use that because some of the users would be so confused by that particular dialog, that it would not serve it's purpose.

But in the right environments, that would work quite well.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top