Solved

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

Posted on 2016-09-29
2
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 70

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

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 …
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

688 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