IMPRESSORA PADRAO

LUCIANORW 29/12/2011 18:03:58
#392140
Boa tarde!!!
Achei varios links nos foruns, aqui mesmo tem varios sobre o assunto.
Com o codigo abaixo, ao carregar o form, exibo uma msg para o usuario trocar a impressora para uma LX-300 (impressão de cheques), só que não é uma caixa [Ô]bonita[Ô] de se ver, achei alguns exemplos que dizem trocar a impressora, só que preciso que ao fechar o form, volte a impressora que estava em uso, mas não sei porque não funciona 100% (até ja fiz isso em outras versões do win/access e funcionava) só que com win 7 + access 2003 (não deve ser o access) simplesmente não se define impressora padrão.
A questão é:
Existe alguma madeira [Ô]apresentavel[Ô] e funcional de se fazer essa troca?
Pensei em carregar as impressoras numa combo, o usuario seleciona a LX-300 e ao sair volto a que estava (carregando a impressora padrão numa variavel), cheguei a fazer isso, mas não redefinia a impressora padrão.
Att
Luciano

Option Compare Database
Option Explicit

Const CCHDEVICENAME = 32
Const CCHFORMNAME = 32
Const GMEM_MOVEABLE = &H2
Const GMEM_ZEROINIT = &H40
Const DM_DUPLEX = &H1000&
Const DM_ORIENTATION = &H1&
Const PD_PRINTSETUP = &H40
Const PD_DISABLEPRINTTOFILE = &H80000

Private Type POINTAPI
x As Long
y As Long
End Type

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Type PAGESETUPDLG
lStructSize As Long
hwndOwner As Long
hDevMode As Long
hDevNames As Long
flags As Long
ptPaperSize As POINTAPI
rtMinMargin As RECT
rtMargin As RECT
hInstance As Long
lCustData As Long
lpfnPageSetupHook As Long
lpfnPagePaintHook As Long
lpPageSetupTemplateName As String
hPageSetupTemplate As Long
End Type

Private Type PRINTDLG_TYPE
lStructSize As Long
hwndOwner As Long
hDevMode As Long
hDevNames As Long
hDC As Long
flags As Long
nFromPage As Integer
nToPage As Integer
nMinPage As Integer
nMaxPage As Integer
nCopies As Integer
hInstance As Long
lCustData As Long
lpfnPrintHook As Long
lpfnSetupHook As Long
lpPrintTemplateName As String
lpSetupTemplateName As String
hPrintTemplate As Long
hSetupTemplate As Long
End Type

Private Type DEVNAMES_TYPE
wDriverOffset As Integer
wDeviceOffset As Integer
wOutputOffset As Integer
wDefault As Integer
extra As String * 100
End Type

Private Type DEVMODE_TYPE
dmDeviceName As String * CCHDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCHFORMNAME
dmUnusedPadding As Integer
dmBitsPerPel As Integer
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type

Private Declare Function PrintDialog Lib [Ô]comdlg32.dll[Ô] Alias [Ô]PrintDlgA[Ô] (pPrintdlg As PRINTDLG_TYPE) As Long
Private Declare Function PAGESETUPDLG Lib [Ô]comdlg32.dll[Ô] Alias [Ô]PageSetupDlgA[Ô] (pPagesetupdlg As PAGESETUPDLG) As Long
Private Declare Sub CopyMemory Lib [Ô]kernel32[Ô] Alias [Ô]RtlMoveMemory[Ô] (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function GlobalLock Lib [Ô]kernel32[Ô] (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib [Ô]kernel32[Ô] (ByVal hMem As Long) As Long
Private Declare Function GlobalAlloc Lib [Ô]kernel32[Ô] (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib [Ô]kernel32[Ô] (ByVal hMem As Long) As Long
Public Sub ShowPrinter(frmOwner As Form, Optional PrintFlags As Long)
[ô]-> Code by Donald Grover
Dim PrintDlg As PRINTDLG_TYPE
Dim DevMode As DEVMODE_TYPE
Dim DevName As DEVNAMES_TYPE

Dim lpDevMode As Long, lpDevName As Long
Dim bReturn As Integer
Dim objPrinter As Printer, NewPrinterName As String

[ô] Use PrintDialog to get the handle to a memory
[ô] block with a DevMode and DevName structures

PrintDlg.lStructSize = Len(PrintDlg)
PrintDlg.hwndOwner = frmOwner.hwnd

PrintDlg.flags = PrintFlags
On Error Resume Next
[ô]Set the current orientation and duplex setting
DevMode.dmDeviceName = Printer.DeviceName
DevMode.dmSize = Len(DevMode)
DevMode.dmFields = DM_ORIENTATION Or DM_DUPLEX
[ô]DevMode.dmPaperWidth = Printer.Width
[ô]DevMode.dmOrientation = Printer.Orientation
[ô]DevMode.dmPaperSize = Printer.PaperSize
[ô]DevMode.dmDuplex = Printer.Duplex
On Error GoTo 0

[ô]Allocate memory for the initialization hDevMode structure
[ô]and copy the settings gathered above into this memory
PrintDlg.hDevMode = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(DevMode))
lpDevMode = GlobalLock(PrintDlg.hDevMode)
If lpDevMode > 0 Then
CopyMemory ByVal lpDevMode, DevMode, Len(DevMode)
bReturn = GlobalUnlock(PrintDlg.hDevMode)
End If

[ô]Set the current driver, device, and port name strings
With DevName
.wDriverOffset = 8
.wDeviceOffset = .wDriverOffset + 1 + Len(Printer.DriverName)
.wOutputOffset = .wDeviceOffset + 1 + Len(Printer.Port)
.wDefault = 0
End With

With Printer
DevName.extra = .DriverName & Chr(0) & .DeviceName & Chr(0) & .Port & Chr(0)
End With

[ô]Allocate memory for the initial hDevName structure
[ô]and copy the settings gathered above into this memory
PrintDlg.hDevNames = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(DevName))
lpDevName = GlobalLock(PrintDlg.hDevNames)
If lpDevName > 0 Then
CopyMemory ByVal lpDevName, DevName, Len(DevName)
bReturn = GlobalUnlock(lpDevName)
End If

[ô]Call the print dialog up and let the usuário make changes
If PrintDialog(PrintDlg) <> 0 Then
[ô]First get the DevName structure.
lpDevName = GlobalLock(PrintDlg.hDevNames)
CopyMemory DevName, ByVal lpDevName, 45
bReturn = GlobalUnlock(lpDevName)
GlobalFree PrintDlg.hDevNames

[ô]Next get the DevMode structure and set the printer
[ô]properties appropriately
lpDevMode = GlobalLock(PrintDlg.hDevMode)
CopyMemory DevMode, ByVal lpDevMode, Len(DevMode)
bReturn = GlobalUnlock(PrintDlg.hDevMode)
GlobalFree PrintDlg.hDevMode
NewPrinterName = UCase$(Left(DevMode.dmDeviceName, InStr(DevMode.dmDeviceName, Chr$(0)) - 1))
If Printer.DeviceName <> NewPrinterName Then
For Each objPrinter In Printers
If UCase$(objPrinter.DeviceName) = NewPrinterName Then
Set Printer = objPrinter
[ô]set printer toolbar name at this point
End If
Next
End If

On Error Resume Next
[ô]Set printer object properties according to selections made
[ô]by user
Printer.Copies = DevMode.dmCopies
Printer.Duplex = DevMode.dmDuplex
Printer.Orientation = DevMode.dmOrientation
Printer.PaperSize = DevMode.dmPaperSize
Printer.PrintQuality = DevMode.dmPrintQuality
Printer.ColorMode = DevMode.dmColor
Printer.PaperBin = DevMode.dmDefaultSource
On Error GoTo 0
End If
End Sub
Private Sub Form_Load()
ShowPrinter Me
End Sub
LUCIANORW 29/12/2011 21:26:47
#392148
Bom, no WinXP funcionou, agora só ano que vem que vou saber se no Win7 funfa.
http://support.microsoft.com/kb/266767/pt-br

FELIZ ANO NOVO A TODOS!!!!!!!!!!

Luciano
Tópico encerrado , respostas não são mais permitidas