Resetting Default Printer in Code
I am currently using VB6 with SP6 installed. I have the Component One add-on also installed. I am using the VSPRINT object to preview/print out a report. There are two basic types of printers that I use.
The two types are:
1 - 8 1/2 inch x 11 inch
2 - 3 inch Receipt Continious
I want to be able to set the default printer temporarily within code so that I can have the report program print to the appropriate printer needed by the specific report that is being requested.
I have this code already there
Public Function SetPrinter(ByVal DeviceName As String) As Boolean
Dim X As Printer
If Printers.Count > 0 Then
For Each X In Printers
MsgBox (X.DeviceName)
If InStr(LCase(X.DeviceName), DeviceName) Then
MsgBox ("Set " & X.DeviceName & " as default printer")
Set Printer = X
SetDefaultPrinter = True
Exit For
End If
Next X
End If
End Function
but even though it shows that the current device set is 8 1/2inch by 11 inch style printer when it goes to the preview it shows it as the default printer which in some cases is quite possible to be the 3inch receipt printer. Therefore if it is the 3inch style it does not display correctly and also when it is told to print it prints to the default printer even though the code above is set such that the default printer within the program should be the one with MAIN in its name which will be the 8 1/2 inch x 11 inch printer.
ANy suggestions or ideas?
Thanks in advance for your help in this.
Seasons Greetings & God Bless to you and yours. :wave:
Re: Resetting Default Printer in Code
Here is how to change windows default printer - you may use the same technic to change it back to whatever it was.
Sample1
Sample2
Re: Resetting Default Printer in Code
Thank you RhinoBull. I am now going to implement this into the code that I am currently working on. Seems that lots of folks are asking about changing the printer through code. Sorry that I seeem to be one of the many who ask. But I am sure very glad that you answered.
Consider this resolved and again thanks for your time and help in this matter.
Re: Resetting Default Printer in Code
Quote:
Originally Posted by DaWolf
Consider this resolved and again thanks for your time and help in this matter.
You're welcome. :wave: Also, since you "Consider this resolved" then you may "officially" do that by pulling down thread's menu ... :) Thanks.
Re: Resetting Default Printer in Code
Its a great solution if you want to enumerate the printers and display it the the user. If you simply just want to change default printers than all you need is this:
VB Code:
Option Explicit
Private Declare Function SetDefaultPrinter Lib "winspool.drv" _
Alias "SetDefaultPrinterA" _
(ByVal pszPrinter As String) As Long
Private Sub Command1_Click()
SetDefaultPrinter "PrinterNameHere"
End Sub
Re: Resetting Default Printer in Code
Quote:
Originally Posted by
Falconsbane
Its a great solution if you want to enumerate the printers and display it the the user. If you simply just want to change default printers than all you need is this:
VB Code:
Option Explicit
Private Declare Function SetDefaultPrinter Lib "winspool.drv" _
Alias "SetDefaultPrinterA" _
(ByVal pszPrinter As String) As Long
Private Sub Command1_Click()
SetDefaultPrinter "PrinterNameHere"
End Sub
Dang Man you are truly Godsend :bigyello: been looking for this for days
tnx alot man