Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-09-29
2
Medium Priority
?
377 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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…
Suggested Courses

609 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