473,624 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Set default printer

sashi
1,754 Recognized Expert Top Contributor
Set default printer

You will often find yourself in a position where you have designed a report format for one printer and when that report is sent to another printer the layout is messed up.

The following code will allow you to specify the printer that reports are sent to by reseting the systems default printer.

Windows API/Global Declarations
Expand|Select|Wrap|Line Numbers
  1. Public Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long
  2.  
  3. Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  4.     Public Const HWND_BROADCAST = &HFFFF&
  5.     Public Const WM_WININICHANGE = &H1A 
  6.  
Code
Expand|Select|Wrap|Line Numbers
  1. Public Function SetDefaultPrinter(objPrn As Printer) As Boolean
  2.     Dim x As Long, sztemp As String
  3.     sztemp = objPrn.DeviceName & "," & objPrn.DriverName & "," & objPrn.Port
  4.     x = WriteProfileString("windows", "device", sztemp)
  5.     x = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0&, "windows")
  6. End Function
  7.  
  8. Private Sub Command1_Click()
  9.     Dim x As Printer
  10.     If MsgBox("Are You Sure Want To Set " & Combo1.Text & " as Default printer ? ", vbYesNo, "Attention") = vbYes Then
  11.  
  12.         For Each x In Printers
  13.             If x.DeviceName = Combo1.Text Then
  14.                 SetDefaultPrinter x
  15.                 Exit Sub
  16.             End If
  17.         Next
  18.  
  19.     End If
  20. End Sub
  21.  
  22. Private Sub Form_Load()
  23.     Dim x As Printer
  24.     Dim y As Integer
  25.     y = 0
  26.  
  27.     With Combo1 'Scan all available printer and put them
  28.         For Each x In Printers 'in To combo box.
  29.             .AddItem x.DeviceName, y
  30.             y = y + 1
  31.         Next
  32.         .ListIndex = 0
  33.     End With
  34.  
  35. End Sub
  36.  
Dec 5 '06 #1
2 17359
creative1
274 Contributor
Good Piece of Work.
Dec 21 '07 #2
delavegas
1 New Member
Absolute amazin. But there a problem, SendMessage(HWN D_BROADCAST, WM_WININICHANGE , 0&, "windows") in sometimes, crash my aplication. Any tip to resolve this problem?!
Jan 6 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
13010
by: Pablo | last post by:
Friends, how can i get the name of the default printer??. I'm working with winforms. VB,C#, nevermind. Thanks a lot Pablo
0
2574
by: Esmee | last post by:
Hi there, I have created an Access 2002 db which contains several reports. Some reports need to be printed on a Laserjet and others on a Labelprinter. One of my clients wants to be able to print the same reports to 3 different laserjets and to 3 different labelprinters. (They work with the db on 3 different units each with their own Laserjet and Labelprinter) For each unit there is a copy of all the reports. The client can make a unit...
2
2101
by: MLH | last post by:
I would like to allow users to set, as an application default, a specific printer to send reports to. I don't know how to bring up a list of valid print devices in A97, nor do I know how to reference any one of them that might be selected. For instance, if I click Start, Control Panel, Printers and Faxes on a PC running XP and I see the following: HP Laser Jet 4200 PCL 6 HP Laser Jet 4200 PCL 5e HP Laser Jet III
2
5583
by: Willem | last post by:
Hi, I'm trying to get the default printer using WMI. I use the following code: Private Function GetDefaultPrinter() As String Dim objWMIService As Object Dim colInstalledPrinters As Object Dim objPrinter As Object
3
25260
by: Raymond Martin | last post by:
How do I find out if the default printer is offline or online (or has a paper jam)?
0
1608
by: Karthick_Microsoft | last post by:
Actually, in my ASP.Net application, i have a requirement of automatically changing the default printer. I did a OCX using VB and put it on the web page. The client machines do not have local printers but network printers (they have been mapped using Local Port pointing to \\machine\printer - this was suggested in a topic - for a reason that a network printer can be given a friendly name) - But when I click on Print from the browser...
1
5448
by: i8mypnuts | last post by:
Could someone please help? I am using the 'defaultprt.zip' tool provided by Ken Getz to change the default printer via VBA code (code below). My problem is that once the default printer has been changed, Access 2003 still directs the report to the former default printer. Access 2003 picks up the new default printer setting only after I have reopened the application. I have checked the default printer setting in the OS control panel and the...
1
2357
by: groulder | last post by:
hi all, i have a problem with a network database that's used by a lot of people. people are finding, they will start the database, and if the mouse is moved over the print icon, the tooltip appears to show which printer it will print to as normal. this printer however is a printer that the user doesn't even have installed.
3
4273
by: brianflannery | last post by:
Greetings all! My problem is this. I've installed a new printer on my computer. This is the "default printer" for access. Now, some of my reports, which were working fine with the old printer, are now showing up with an odd view (very enlarged) in preview. If I go to page setup, and choose specific printer to then look at the paper size listed in my default printer, it shows Hegaki Card 100mmX148mm instead of letter. It seems as...
0
8249
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8633
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8348
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4187
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1493
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.