RE: how to change prniter settings

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Gary Chang[MSFT] (v-garych_at_online.microsoft.com)
Date: 12/15/04


Date: Wed, 15 Dec 2004 09:56:01 GMT

Hi Jean,

I suggest you take a look on the Win32 API: SetPrinter

By the way, some code sample:

Private Declare Function OpenPrinter Lib "winspool.drv" Alias
"OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault
As PRINTER_DEFAULTS) As Long
Private Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA"
(ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal
Command As Long) As Long
Private Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA"
(ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf
As Long, pcbNeeded As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest
As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As
Long) As Long
Private Declare Function DocumentProperties Lib "winspool.drv" Alias
"DocumentPropertiesA" (ByVal hwnd As Long, ByVal hPrinter As Long, ByVal
pDeviceName As String, ByVal pDevModeOutput As Any, ByVal pDevModeInput As
Any, ByVal fMode As Long) As Long
Public Sub SetPrinterPage(PaperInfo As PaperInfoType)
Dim PrinterHandle As Long
Dim PrinterName As String
Dim pd As PRINTER_DEFAULTS
Dim MyDevMode As DEVMODE
Dim Result As Long
Dim Needed As Long
Dim pFullDevMode As Long
PrinterName = Printer.DeviceName
If PrinterName = "" Then
Exit Sub
End If
pd.pDatatype = vbNullString
pd.pDevMode = 0&
pd.DesiredAccess = PRINTER_ALL_ACCESS
Result = OpenPrinter(PrinterName, PrinterHandle, pd)
Result = GetPrinter(PrinterHandle, 2, ByVal 0&, 0, Needed)
ReDim pi2_buffer((Needed \ 4))
Result = GetPrinter(PrinterHandle, 2, pi2_buffer(0), Needed, Needed)
pFullDevMode = pi2_buffer(7)
Call CopyMemory(MyDevMode, ByVal pFullDevMode, Len(MyDevMode))
MyDevMode.dmDuplex = DM_DUPLEX
MyDevMode.dmOrientation = PaperInfo.Orientation
On Error Resume Next
If PaperInfo.PaperSize = 0 Or PaperInfo.PaperSize = 256 Then
MyDevMode.dmFields = DM_DUPLEX Or DM_ORIENTATION Or DM_PAPERLENGTH Or
DM_PAPERWIDTH Or DM_PAPERSIZE
MyDevMode.dmPaperSize = 256
MyDevMode.dmPaperLength = PaperInfo.PaperHeight
MyDevMode.dmPaperWidth = PaperInfo.PaperWidth
Else
MyDevMode.dmFields = DM_DUPLEX Or DM_ORIENTATION Or DM_PAPERSIZE
MyDevMode.dmPaperSize = PaperInfo.PaperSize
End If
Call CopyMemory(ByVal pFullDevMode, MyDevMode, Len(MyDevMode))
'Copy our changes to "the PUBLIC portion of the DevMode" into "the PRIVATE
portion of the DevMode"
Result = DocumentProperties(fMainForm.hwnd, PrinterHandle, PrinterName,
ByVal pFullDevMode, ByVal pFullDevMode, DM_IN_BUFFER Or DM_OUT_BUFFER)

'Update the printer's default properties (to verify, go to the Printer
folder
' and check the properties for the printer)
Result = SetPrinter(PrinterHandle, 2, pi2_buffer(0), 0&)

Call ClosePrinter(PrinterHandle)

'Note: Once "Set Printer = " is executed, anywhere in the code, after that
point
' changes made with SetPrinter will ONLY affect the system-wide printer --
' -- the changes will NOT affect the VB printer object.
' Therefore, it may be necessary to reset the printer object's parameters to
' those chosen in the devmode.
Dim p As Printer
For Each p In Printers
If p.DeviceName = PrinterName Then
Set Printer = p
Exit For
End If
Next p
' Printer.Duplex = MyDevMode.dmDuplex
Dim iret As Long
If Left(MyOsInfo.szCSDVersion, 1) = Chr(0) Then
MyOsInfo.dwOSVersionInfoSize = Len(MyOsInfo)
iret = GetVersionEx(MyOsInfo)
End If
On Error Resume Next
If MyOsInfo.dwPlatformId <> VER_PLATFORM_WIN32_NT Then
Printer.Orientation = IIf(PaperInfo.Orientation = 0, 1,
PaperInfo.Orientation)
If PaperInfo.PaperSize = 0 Or PaperInfo.PaperSize = 256 Then
Printer.Height = PaperInfo.PaperHeight * 5.67
Printer.Width = PaperInfo.PaperWidth * 5.67
Else
Printer.PaperSize = PaperInfo.PaperSize
End If
End If
End Sub

Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------



Relevant Pages