ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Finding the default printer (https://www.excelbanter.com/excel-programming/343602-finding-default-printer.html)

Ben[_10_]

Finding the default printer
 
Hello,
Is there any way that I can specify that a selection should be printed to
the default printer rather than the last printer that was used.
Thank you in advance



Leith Ross[_98_]

Finding the default printer
 

Hello Ben,

I knew I had this code. I just didn't remember where I saved it. The
macro returns the name of the default printer. Once you have that you
can use VBA to the active printer to the default printer.

EXAMPLE:

P = GetDefaultPrinter
Application.ActivePrinter = P


Code:
--------------------
'///////////////////////////////////////////'
'/ /'
'/ Get the Default Printer Information /'
'/ For Windows '95, '98, ,Me ,NT, 2000, XP /'
'/ /'
'/ Written: October 23, 2005 /'
'/ Author: Leith Ross /'
'/ /'
'///////////////////////////////////////////'

'===================NOTE========================== =======
'Windows Server 2003 and Windows XP/2000/NT:
'Calls to profile functions may be mapped to the
'registry instead of to the initialization files.
'This mapping occurs when the initialization file
'and section are specified in the registry under
'the following keys:
'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\
' CurrentVersion\IniFileMapping
'
'
'When the operation has been mapped, the GetProfileString
'function retrieves information from the registry, not
'from the initialization file; the change in the storage
'location has no effect on the function's behavior.
'================================================= ========


Private Declare Function GetProfileString _
Lib "kernel32.dll" _
Alias "GetProfileStringA" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long) As Long


Public Function GetDefaultPrinter() As String

Dim Buffer As String
Dim ByteCount As Long
Dim MaxCount As Long
Dim DefaultInfo As String

MaxCount = 260
Buffer = String(MaxCount, Chr$(0))

'Retreive current default printer information
ByteCount = GetProfileString("windows", "device", ",,,", Buffer, MaxCount)

DefaultInfo = Left(Buffer, ByteCount)

'Remove second item between commas and replace with "on"
I = InStr(1, DefaultInfo, ",")
LeftSide = Left(DefaultInfo, I - 1)
I = InStr(I + 1, DefaultInfo, ",")
RightSide = Right(DefaultInfo, ByteCount - I)
DefaultInfo = LeftSide & " on " & RightSide

GetDefaultPrinter = DefaultInfo

End Function

--------------------


Sincerely,
Leith Ross


--
Leith Ross
------------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
View this thread: http://www.excelforum.com/showthread...hreadid=478631



All times are GMT +1. The time now is 02:53 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com