Click here to Skip to main content
15,037,662 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I try to switch default printer but I dont known what is wrong. Can you help me?

What I have tried:

My code:

Private Function DefaultPrinterName1(sender As Object, e As EventArgs)

        Dim oPS As New System.Drawing.Printing.PrinterSettings
            DefaultPrinterName1 = oPS.PrinterName = "Vario III 107/12 (kopie 1)"
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Capturing Default Printer", MessageBoxButtons.OK)
            oPS = Nothing
        End Try
    End Function
Updated 28-May-19 17:18pm
lw@zi 24-Jul-18 9:00am
Can you add error message you are getting?
[no name] 24-Jul-18 9:05am
no error, nothing will happen
[no name] 24-Jul-18 9:06am
I have a button where I call this function

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
DefaultPrinterName1(sender, e)
End Sub

Your code will not change the default printer outside your application. If you want to change the default printer, you will need to make use of interop. Following method in particular:

[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetDefaultPrinter(string Name);

<DllImport("winspool.drv", CharSet:=CharSet.Auto, SetLastError:=True)>
Public Shared Function SetDefaultPrinter(Name As String) As Boolean
End Function

You can then call this function with the name of printer. Note that the printer you are specifying should already be installed on the computer.
[no name] 24-Jul-18 9:23am
and where I can write this part of code? <DllImport("winspool.drv", CharSet:=CharSet.Auto, SetLastError:=True)>
lw@zi 24-Jul-18 9:28am
This needs to be in the form class of yours. Similar to other methods that you would have created
[no name] 26-Jul-18 0:15am
after I put this to my code that show many errors: dllimport is not defined, charset not defined
lw@zi 26-Jul-18 0:38am
You will need to add import statements for System.Runtime.InteropServices.
[no name] 26-Jul-18 2:45am
thank you :)
lw@zi 26-Jul-18 3:18am
No problem.
Shell(String.Format("rundll32 printui.dll,PrintUIEntry /y /n ""{0}""", nameprinter1))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900