Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Code Printer Properties
Code Printer Properties
of my own projects!
As the printer driver cannot be directly addressed one simply uses SENDKEYS. Every printer driver that conforms to the standard
spec. has keyboard shortcuts to select all of the functions using only the keyboard. Try the following :
Sub HP_Paper_Source()
'Application.Dialogs(xlDialogPrint).Show
Application.Wait Now + TimeValue("00:00:01")
Application.ActivePrinter = "HP DeskJet 870Cxi on LPT1:"
SendKeys "%(f)(p)(r)%(s)%(s){pgup}{down}{down}{down}{down}~~~"
End Sub
Sub PrinterDialog()
Application.Dialogs(xlDialogPrint).Show ' Show printer dialog box
End Sub
This macro is not correct for this particular printer, but it works if you manually write down the keys that you need to access a
particular setting, and substitute them in the above code.It's easiest to do this character by character.
% = press ALT
^ = ctrl
~ = return
Richard
krinid (Programmer)
(OP)
28 Sep 03 19:03
Richard,
Wow, two months later and you came back to answer my question! Thanks.
tbl (TechnicalUser)29 Sep 03 01:31
Krinid,
I hope it solves your problem. I think this forum is terrific, and like to put as much back as possible. It's certainly helped me
hundreds of times.
By the way, I put in the second short routine to indicate how one calls up the printer dialog, although it is only useful for debugging
and is not needed for setting up the printer. I had the same problem with setting a set of networked Xerox copier/printer/fax
machines to print to Fax. This instruction probably could be addressed directly by VBA code if one only knew what the code was !
But with SENDKEYS one can work through the various pages of settings until Fax is reached, without any special knowledge, just
patience.
Richard
Nelviticus (Programmer)29 Sep 03 06:58
There's a much better way than using SendKeys. I found a couple of VBA functions on a web site that call the Windows APIs to
change lots of printer settings. You don't need to understand these functions, you just put them into a module and call them from
your own procedures.
I can no longer remember the link but fortunately I copied the code and I've pasted it at the end of this post. The functions you'll
be calling are the ones defined as Public - i.e. GetDuplex, SetDuplex and so on.
One word of warning though - many printer drivers don't follow the Windows API guidelines properly, so you'll have to check that
when you set the Duplex mode (for instance) it actually sets it to what you want.
Finally, these functions change the default printer settings, so you'd normally want to Get the current settings and store them in
variables so you can reset them when your code is done.
The code follows (it's long, but you just need to copy & paste it):
ÿ
Option Explicit
pd.DesiredAccess = PRINTER_NORMAL_ACCESS
iRet = OpenPrinter(sPrinterName, hPrinter, pd)
If (iRet = 0) Or (hPrinter = 0) Then
'Can't access current printer. Bail out doing nothing
Exit Function
End If
cleanup:
'Release the printer handle
If (hPrinter <> 0) Then Call ClosePrinter(hPrinter)
pd.DesiredAccess = PRINTER_NORMAL_ACCESS
'Find out how many bytes needed for the printer properties
iRet = DocumentProperties(0, hPrinter, sPrinterName, 0, 0, 0)
If (iRet < 0) Then
'Couldn't access printer properties
GoTo cleanup
End If
cleanup:
'Release the printer handle
If (hPrinter <> 0) Then Call ClosePrinter(hPrinter)
End Function
ÿ