0% found this document useful (0 votes)
98 views3 pages

Change Default Printer

The user asked how to change the default printer or make a PrintPreviewDialog use a specific printer in VB.NET. Eric Blackwelder responded with code samples to: 1) Get a list of available printers on the network 2) Change the printer settings for the current print job 3) Change the default Windows printer by modifying the Windows registry He provided the full code for each solution. Others thanked him for the helpful response.

Uploaded by

Aman Mittal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views3 pages

Change Default Printer

The user asked how to change the default printer or make a PrintPreviewDialog use a specific printer in VB.NET. Eric Blackwelder responded with code samples to: 1) Get a list of available printers on the network 2) Change the printer settings for the current print job 3) Change the default Windows printer by modifying the Windows registry He provided the full code for each solution. Others thanked him for the helpful response.

Uploaded by

Aman Mittal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

i all. Forgive me if this has been answered before.

I did lots of searching, and only f ound answers for vb6, not vb.net. I have a vb.net app that uses PrintPreviewDialog to print a document. Now I want the user to be able to print to any printer on the network. So, I created a lis tbox of available printers which works fine. Now, I need to either change the wi ndows default printer to the one selected, or make the PrintPreviewDialog use th at printer name. I have tried many things to no avail. Any sugggestions would be greatly apprecia ted. Thanks. Eric Post Points: 35 eblackwelder West Springfield, MA Since 1/16/2003 Posts 165 Reputation 2,190 Reply Eric Blackwelder (eblackwelder) replied on 6/4/2004 8:40 AM rated by 0 users Nevermind. Found the problem - pebkac. :) Post Points: 5 dries_neyrinck Lovely Ghent in Belgium Since 4/28/2003 Posts 4,227 Reputation 30,780 Reply dries neyrinck (dries_neyrinck) replied on 6/4/2004 9:43 AM rated by 0 users It would be great if you posted your solution or a description how to do it, to help other forumites that might have this question in the future. ;-) thanks, D Post Points: 5 eblackwelder West Springfield, MA Since 1/16/2003 Posts 165 Reputation 2,190 Reply Eric Blackwelder (eblackwelder) replied on 6/4/2004 10:25 AM rated by 0 users Yes, thanks D. What was I thinking? It is definately Friday - I usually do that,

but my mind is elsewhere. First, you need this up top: Code: Imports System.Drawing.Printing Next, to get a list of available printers: Code: 'cboPrinter Dim Dim For .PrinterName Next Now, two solutions to change the printer: 1. This just changes the printer for the document you want to print: Code: Private Sub pdCrRpt_PrintPage(ByVal sender As Object, ByVal e As System.Draw ing.Printing.PrintPageEventArgs) Handles pdCrRpt.PrintPage 'code for setting up print document End Sub 'in another sub: Dim ppdPreview As New PrintPreviewDialog() ppdPreview.Document = pdCrRpt ppdPreview.Document.PrinterSettings.PrinterName = strPrinterName 'where strPrinterName contains the name of the printer you want 'this is the only change I made to my existing code to make my idea work ppdPreview.ShowDialog() 2. This actually changes your windows default printer: (I got this to work, but used the first solution as it is simpler) Code: Private Sub SetDefaultPrinter(ByVal PrinterName As String, _ ByVal DriverName As String, ByVal PrinterPort As String) Dim DeviceLine As String 'rebuild a valid device line string DeviceLine = PrinterName & "," & DriverName & "," & PrinterPort 'Store the new printer information in the '[WINDOWS] section of the WIN.INI file for 'the DEVICE= item Call WriteProfileString("windows", "Device", DeviceLine) is a combobox printers As New Printing.PrintDocument() printername = printers.PrinterSettings.PrinterName Each printername In printers.PrinterSettings.InstalledPrinters cboPrinter.Items.Add(printername) cboPrinter.SelectedItem = pdTest.DefaultPageSettings.PrinterSettings

'Cause all applications to reload the INI file Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows") End Sub Private Declare Function WriteProfileString Lib "kernel32" Alias "WriteP rofileStringA" _ (ByVal lpszSection As String, ByVal lpszKeyName As String, _ ByVal lpszString As String) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, ByVal lparam As String) As Long Private Const HWND_BROADCAST As Long = &HFFFF& Private Const WM_WININICHANGE As Long = &H1A 'in a sub: SetDefaultPrinter(strPrinterName, "", "") 'where strPrinterName contains the name of the printer you want Eric Post Points: 20 ladendecker Springfield, Ma Since 6/16/2003 Posts 25 Reputation 305 Reply Richard Ladendecker (ladendecker) replied on 8/8/2004 7:05 PM rated by 0 users Nice routine Eric.. I need one to do this. Compliments Rick.. Post Points: 5 AnharMiah Since 6/12/2011 Posts 1 Reputation 5 Reply AnharMiah (AnharMiah) replied on 6/12/2011 6:35 AM rated by 0 users Hello I thought I register to reply:

There is an easier one liner to set the default printer:

view plaincopy to clipboardprint? Shell(String.Format("rundll32 printui.dll,PrintUIEntry /y /n ""{0}""", PrinterNa me))

You might also like