Solved

Change default printer using Visual Basic .net application in Windows 10

Posted on 2016-09-29
2
42 Views
Last Modified: 2016-09-29
Hello,

I have a legacy application, which ran fine as below.
It was able to retrieve the current default printer, change the default printer, print to the NEW default printer and change back to the old default printer with the below code.

Now, I have installed the app to a Windows 10 PC and it prints fine to the CURRENT default printer, but it does not change the default printer any more.

Any advise? I am developing on Visual Studio 2015 in a Windows 10 PRO environment (64 bit).

The listed printer I want to print on is on a machine WHUPS2366. If I go into my Windows Print settings manually and make that printer the default printer. It works fine. However, since the target PC for the application prints to multiple PCs, I need to have my application to select the printer.
Dim labelprinter As String = "\\WHUPS2366\UPS Thermal 2844"
            Dim prtdoc As New PrintDocument()
            Dim strDefaultPrinter As String = prtdoc.PrinterSettings.PrinterName
            Dim strPrinter As String
            Dim oldprinter As String = ""
            For Each strPrinter In PrinterSettings.InstalledPrinters

                If strPrinter = strDefaultPrinter Then
                    oldprinter = strPrinter
                Else
                End If
            Next strPrinter

            Dim WshNetwork As Object
            WshNetwork = Microsoft.VisualBasic.CreateObject("WScript.Network")
            WshNetwork.AddWindowsPrinterConnection(labelprinter)
            WshNetwork.SetDefaultPrinter(labelprinter)
            WshNetwork = Nothing
            Dim pkPaperSize As New Printing.PaperSize("ups2labels", 400, 850)
            PrintDocument1.DefaultPageSettings.PaperSize = pkPaperSize
            PrintDocument1.Print()
            WshNetwork = Microsoft.VisualBasic.CreateObject("WScript.Network")
            WshNetwork.SetDefaultPrinter(oldprinter)
            WshNetwork = Nothing

Open in new window

0
Comment
Question by:zettler92656
2 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41822317
I have a couple of ways listed at http://emoreau.com/Entries/Articles/2005/03/Setting-Windows-default-printer.aspx

I haven't tried specifically on Windows 10 but one of them must work!

Quick question: do you run your application with administrator privileges?
0
 

Author Closing Comment

by:zettler92656
ID: 41822438
I updated the code to
[CODE]
            Dim labelprinter As String = "\\WHUPS2366\UPS Thermal 2844"
            Dim pkPaperSize As New Printing.PaperSize("ups2labels", 400, 850)
            PrintDocument1.DefaultPageSettings.PaperSize = pkPaperSize
            PrintDocument1.PrinterSettings.PrinterName = labelprinter
            PrintDocument1.Print()
[/CODE]

So basically, I am not changing the Default printer at all. This works actually better.

Thank you.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Printers have changed substantially in the last 30 or so years, not just in technical capabilities but in cost and usage as well.  Printers were originally used for interfacing with the operator, not necessarily for printing copy or pictures. In …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now