Changing printers in Access 97 using VBA
Changing printers in Access 97 using VBA
(OP)
I'm stuck in my Access application for changing the printer for printing reports. I have found code for Access 2000, but this doesn't work in 97. Can anyone guide me how to change my printer in Access 97?
cheers
Steven
cheers
Steven





RE: Changing printers in Access 97 using VBA
The following is some printer code that I've used, and I don't know if it will work in A97, but you're welcome to give it a shot.
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.
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.
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:
Private Sub cboPrinters_AfterUpdate()
SetPrinterAsDefault cboPrinters.Text
End Sub
And in the module, add the SetPrinterAsDefault function
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