locked
How to Change the Default Printer RRS feed

  • Question

  • How do we programmatically change a computers default printer if we know the name of the printer that we want to make the default printer?

    Jeffrey Doiron

    Tuesday, April 19, 2016 2:56 PM

Answers

  • How do we programmatically change a computers default printer if we know the name of the printer that we want to make the default printer?

    Jeffrey Doiron

    The code in the ComboBox SelectedIndex Changed event can be used in some other event (Button Click) by providing a string for the printer name in question rather than using "ComboBox1.Text".

    Option Strict On
    
    Imports System.Management ' Add reference, Project Tab, Add reference, Assemblies, Framework, check System.Management
    
    Public Class Form1
    
        Dim search As System.Management.ManagementObjectSearcher
    
        Dim results As System.Management.ManagementObjectCollection
    
        Dim args(1) As Object
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
    
            Try
    
                search = New System.Management.ManagementObjectSearcher("select * from win32_printer")
    
                results = search.Get()
    
                For Each printer In results
                    ComboBox1.Items.Add(printer("Name").ToString)
                    ComboBox2.Items.Add(printer.Item("Default").ToString)
                Next
    
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString())
            End Try
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            Try
                For Each printer As Management.ManagementObject In results
    
                    If printer("Name").ToString = ComboBox1.Text Then
    
                        printer.InvokeMethod("SetDefaultPrinter", CType(args(0), Object()))
    
                    ElseIf (ComboBox1.SelectedIndex.Equals(-1)) Then
                        MessageBox.Show("Nothing is selected in ComboBox1." & vbCrLf & "Please select a printer in ComboBox1 dropdown and try again")
                    End If
                Next
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString())
            End Try
        End Sub
    
    End Class



    La vida loca

    Tuesday, April 19, 2016 3:08 PM
  •  You could also use the Win32 SetDefaultPrinter function to set the default printer by its name.  Below is an example,  it loads the names of the installed printers into a ComboBox and selects the current default printer when the form loads.  You can then select another printer in the ComboBox and then press the Button to set it as the default printer.

    Imports System.Runtime.InteropServices
    Imports System.Drawing.Printing

    Public Class Form1
        <DllImport("Winspool.drv", EntryPoint:="SetDefaultPrinterW")>
        Private Shared Function SetDefaultPrinterW(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszPrinter As String) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
            For Each p As String In PrinterSettings.InstalledPrinters
                ComboBox1.Items.Add(p)
            Next
            Dim ps As New PrinterSettings
            ComboBox1.SelectedItem = ps.PrinterName
        End Sub

        Private Sub Button_SetDefaultPrinter_Click(sender As Object, e As EventArgs) Handles Button_SetDefaultPrinter.Click
            Dim ps As New PrinterSettings
            If ComboBox1.SelectedIndex > -1 AndAlso ComboBox1.SelectedItem.ToString <> ps.PrinterName Then
                If Not SetDefaultPrinterW(ComboBox1.SelectedItem.ToString) Then
                    MessageBox.Show("Could not set selected printer to default.", "Error...")
                End If
            End If
        End Sub
    End Class


    If you say it can`t be done then i`ll try it

    Tuesday, April 19, 2016 11:39 PM

All replies

  • How do we programmatically change a computers default printer if we know the name of the printer that we want to make the default printer?

    Jeffrey Doiron

    The code in the ComboBox SelectedIndex Changed event can be used in some other event (Button Click) by providing a string for the printer name in question rather than using "ComboBox1.Text".

    Option Strict On
    
    Imports System.Management ' Add reference, Project Tab, Add reference, Assemblies, Framework, check System.Management
    
    Public Class Form1
    
        Dim search As System.Management.ManagementObjectSearcher
    
        Dim results As System.Management.ManagementObjectCollection
    
        Dim args(1) As Object
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
    
            Try
    
                search = New System.Management.ManagementObjectSearcher("select * from win32_printer")
    
                results = search.Get()
    
                For Each printer In results
                    ComboBox1.Items.Add(printer("Name").ToString)
                    ComboBox2.Items.Add(printer.Item("Default").ToString)
                Next
    
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString())
            End Try
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            Try
                For Each printer As Management.ManagementObject In results
    
                    If printer("Name").ToString = ComboBox1.Text Then
    
                        printer.InvokeMethod("SetDefaultPrinter", CType(args(0), Object()))
    
                    ElseIf (ComboBox1.SelectedIndex.Equals(-1)) Then
                        MessageBox.Show("Nothing is selected in ComboBox1." & vbCrLf & "Please select a printer in ComboBox1 dropdown and try again")
                    End If
                Next
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString())
            End Try
        End Sub
    
    End Class



    La vida loca

    Tuesday, April 19, 2016 3:08 PM
  •  You could also use the Win32 SetDefaultPrinter function to set the default printer by its name.  Below is an example,  it loads the names of the installed printers into a ComboBox and selects the current default printer when the form loads.  You can then select another printer in the ComboBox and then press the Button to set it as the default printer.

    Imports System.Runtime.InteropServices
    Imports System.Drawing.Printing

    Public Class Form1
        <DllImport("Winspool.drv", EntryPoint:="SetDefaultPrinterW")>
        Private Shared Function SetDefaultPrinterW(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszPrinter As String) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
            For Each p As String In PrinterSettings.InstalledPrinters
                ComboBox1.Items.Add(p)
            Next
            Dim ps As New PrinterSettings
            ComboBox1.SelectedItem = ps.PrinterName
        End Sub

        Private Sub Button_SetDefaultPrinter_Click(sender As Object, e As EventArgs) Handles Button_SetDefaultPrinter.Click
            Dim ps As New PrinterSettings
            If ComboBox1.SelectedIndex > -1 AndAlso ComboBox1.SelectedItem.ToString <> ps.PrinterName Then
                If Not SetDefaultPrinterW(ComboBox1.SelectedItem.ToString) Then
                    MessageBox.Show("Could not set selected printer to default.", "Error...")
                End If
            End If
        End Sub
    End Class


    If you say it can`t be done then i`ll try it

    Tuesday, April 19, 2016 11:39 PM
  •  You could also use the Win32 SetDefaultPrinter function to set the default printer by its name.  Below is an example,  it loads the names of the installed printers into a ComboBox and selects the current default printer when the form loads.  You can then select another printer in the ComboBox and then press the Button to set it as the default printer.

    Public Class Form1
        <DllImport("Winspool.drv", EntryPoint:="SetDefaultPrinterW")>
        Private Shared Function SetDefaultPrinterW(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszPrinter As String) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For Each p As String In PrinterSettings.InstalledPrinters
                ComboBox1.Items.Add(p)
            Next
            Dim ps As New PrinterSettings
            ComboBox1.SelectedItem = ps.PrinterName
        End Sub
    
        Private Sub Button_SetDefaultPrinter_Click(sender As Object, e As EventArgs) Handles Button_SetDefaultPrinter.Click
            Dim ps As New PrinterSettings
            If ComboBox1.SelectedIndex > -1 AndAlso ComboBox1.SelectedItem.ToString <> ps.PrinterName Then
                If Not SetDefaultPrinterW(ComboBox1.SelectedItem.ToString) Then
                    MessageBox.Show("Could not set selected printer to default.", "Error...")
                End If
            End If
        End Sub
    End Class


    If you say it can`t be done then i`ll try it


    Nice.

    La vida loca

    Wednesday, April 20, 2016 1:10 AM