473,468 Members | 1,586 Online
Bytes | Software Development & Data Engineering Community
Create 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 17300
creative1
274 Contributor
Good Piece of Work.
Dec 21 '07 #2
delavegas
1 New Member
Absolute amazin. But there a problem, SendMessage(HWND_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
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
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...
2
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...
2
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...
3
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
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...
1
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...
1
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...
3
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,...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
1
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.