liljegren
asked on
Change printer
Hello experts. I need a way to change the current printer in VB. I don't want to change the default printer in Windows, only the printer the VB app uses.
I collect some parameters like duplex and color in a ini-file, and I thought of storing the printername similary, but I don't know how to do it. The idea is that, without messing with the Windows default printer, the setting I do in the app should be stored, so I don't have to set the printer next time I use the app.
I collect some parameters like duplex and color in a ini-file, and I thought of storing the printername similary, but I don't know how to do it. The idea is that, without messing with the Windows default printer, the setting I do in the app should be stored, so I don't have to set the printer next time I use the app.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oh well I was littel late!
As for storing the printer selected, you can use the SaveSetting function, as in the following subroutine:
Sub SetAppPrinter(DevName As String)
Dim X As Printer
For Each X In Printers
If X.DeviceName = DevName Then
' Set printer as the apps printer.
Set Printer = X
' Stop looking for a printer.
Exit For
End If
Next
SaveSetting App.ProductName, "Startup", "Printer", Printer.DeviceName
End Sub
When you pass the device name to the subroutine, it sets the printer and saves the information.
Then you can get back the saved printer name by using:
Dim Pname As String
Pname= GetSetting App.ProductName, "Startup", "Printer", Printer.deviceName
If a SaveSetting has been run, you get the results of it... Otherwise you get the systems's default printer...
Cheers!
Sub SetAppPrinter(DevName As String)
Dim X As Printer
For Each X In Printers
If X.DeviceName = DevName Then
' Set printer as the apps printer.
Set Printer = X
' Stop looking for a printer.
Exit For
End If
Next
SaveSetting App.ProductName, "Startup", "Printer", Printer.DeviceName
End Sub
When you pass the device name to the subroutine, it sets the printer and saves the information.
Then you can get back the saved printer name by using:
Dim Pname As String
Pname= GetSetting App.ProductName, "Startup", "Printer", Printer.deviceName
If a SaveSetting has been run, you get the results of it... Otherwise you get the systems's default printer...
Cheers!
You will also need a way to test the port as well, since there can be multiple devicename listings with different ports.
For Each X In Printers
' Set printer as system default.
Debug.Print X.DeviceName
'If (X.DeviceName = whatyouarelookingfor) Then
'Set Printer = X
'Exit For
'End If
Next