VB.NET: Setting Default Printer with Visual Basic Code


When we create application for transaction that sending print command directly to printer, it sends to default printer. I use it for my Point of Sales (POS) Application printing. Just like I shown you on this article.

Actually, user can set their default printer in Control Panel -> Device and Printer as below picture. Choose any printer you want then right click on its icon, set as default printer.

However, your app will be better and more user friendly if they can set default printer with your app feature.


So, this time I want to show you a sample code how to create setting default printer with VB.NET code.

UI



Code:

Imports System.Drawing.Printing

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles MyBase.Load

        Dim strInstalledPrinters As String
        Dim prntDoc As New PrintDocument

        'check if there is installed printer
        If PrinterSettings.InstalledPrinters.Count = 0 Then
            MsgBox("No printer installed")
            Exit Sub
        End If


        'display installed printer into combobox list item
        For Each strInstalledPrinters In PrinterSettings.InstalledPrinters
            ComboBox1.Items.Add(strInstalledPrinters)
        Next strInstalledPrinters

        'Display current default printer on combobox texts
        ComboBox1.Text = prntDoc.PrinterSettings.PrinterName

        Button1.Text = "Set Default Printer"


    End Sub

    'Function to set a printer as default
    Function SetDefaulPrinter(ByVal strPrinterName As String) As Boolean
        Dim strCurrPrinter As String
       
Dim WsNetwork As Object
       
Dim prntDoc As New PrintDocument

        strCurrPrinter = prntDoc.PrinterSettings.PrinterName
        WsNetwork = Microsoft.VisualBasic.CreateObject("WScript.Network")

        Try
            WsNetwork.SetDefaultPrinter(strPrinterName)
            prntDoc.PrinterSettings.PrinterName = strPrinterName

            'set default if selected printer name is a valid installed printer
            If prntDoc.PrinterSettings.IsValid Then
                Return True
            Else
                WsNetwork.SetDefaultPrinter(strCurrPrinter)
                Return False
            End If

        Catch ex As Exception
            WsNetwork.SetDefaultPrinter(strCurrPrinter)
            Return False
        Finally
            WsNetwork = Nothing
            prntDoc = Nothing
        End Try
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles Button1.Click
        If SetDefaulPrinter(ComboBox1.Text) = True Then
            MsgBox("Printer default menjadi " & ComboBox1.Text)
        Else
            MsgBox("Printer name " & ComboBox1.Text & " is not valid!")
        End If
    End Sub

End Class


Runtime:

List of installed printer is displayed on item list of combobox.


Choose one of printer then click "Set Default Printer" button.


After succeed notification prompted, lets check on device and printer, default printer will be changed.



Post a Comment

0 Comments