Solved

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

Posted on 2016-09-29
2
63 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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 …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 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

22 Experts available now in Live!

Get 1:1 Help Now