Solved

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
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…

734 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