IMPRESSORA PADRÃO
Módulo:
Public Function GetDefaultPrinter() As Printer [ô]Rotina que verifica a impressora padrão
Dim strBuffer As String * 254
Dim iRetValue As Long
Dim strDefaultPrinterInfo As String
Dim tblDefaultPrinterInfo() As String
Dim objPrinter As Printer
iRetValue = GetProfileString([Ô]windows[Ô], [Ô]device[Ô], [Ô],,,[Ô], strBuffer, 254) [ô]Busca informações da impressora padrão
strDefaultPrinterInfo = Left(strBuffer, InStr(strBuffer, Chr(0)) - 1)
tblDefaultPrinterInfo = Split(strDefaultPrinterInfo, [Ô],[Ô])
For Each objPrinter In Printers
If objPrinter.DeviceName = tblDefaultPrinterInfo(0) Then
Exit For [ô]Se achou a impressora padrão então sai
End If
Next
If objPrinter.DeviceName <> tblDefaultPrinterInfo(0) Then [ô]Se não achou a impressora retorna nothing
Set objPrinter = Nothing
End If
Set GetDefaultPrinter = objPrinter
End Function [ô]Fim da rotina
Quando abro o formMDI, coloco o caption para ele aparecer o nome da impressora padrão:
Caption = [Ô]Menu Principal - [Impressora Padrão: [Ô] & GetDefaultPrinter & [Ô]][Ô]
Porém ocorre o seguinte erro.
Alguém sabe como?
Dim r As Long
Dim Buffer As String
[ô] Get the list of available printers from WIN.INI
Buffer = Space(8192)
r = GetProfileString([Ô]PrinterPorts[Ô], vbNullString, [Ô][Ô], _
Buffer, Len(Buffer))
[ô] Display the list of printer in the ListBox List1
ParseList combo_impressoras, Buffer
[ô]informa a impressora atual selecionada
atual.Caption = Printer.DeviceName
End Sub
Private Sub combo_impressoras_GotFocus()
combo_impressoras.SelStart = 0
combo_impressoras.BackColor = &HFFFF&
End Sub
Private Sub combo_impressoras_KeyDown(KeyCode As Integer, Shift As Integer)
If PosicaoDoCombo = 0 Then If KeyCode = 40 Then SendKeys [Ô]{F4}[Ô]: PosicaoDoCombo = 1
End Sub
Private Sub combo_impressoras_KeyPress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub
Private Sub combo_impressoras_LostFocus()
combo_impressoras.BackColor = &H80000005
End Sub
Private Sub Command1_Click()
If combo_impressoras.Text = Empty Then
MsgBox [Ô]Selecione uma opção de impressora na lista ...[Ô], vbExclamation
combo_impressoras.SetFocus
Else
DoEvents
Me.MousePointer = 11
DoEvents
Dim osinfo As OSVERSIONINFO
Dim retValue As Integer
osinfo.dwOSVersionInfoSize = 148
osinfo.szCSDVersion = Space$(128)
retValue = GetVersionExA(osinfo)
Call WinNTSetDefaultPrinter
atual.Caption = Printer.DeviceName
atual.BackColor = &HFFFF&
info01.Caption = [Ô]Nova impressora :[Ô]
info01.BackColor = &HFFFF&
DoEvents
Me.MousePointer = 0
End If
End Sub
[ô]definição da impressora do sistema
Private Sub SetDefaultPrinter(ByVal PrinterName As String, _
ByVal DriverName As String, ByVal PrinterPort As String)
Dim DeviceLine As String
Dim r As Long
Dim l As Long
DeviceLine = PrinterName & [Ô],[Ô] & DriverName & [Ô],[Ô] & PrinterPort
[ô] Store the new printer information in the [WINDOWS] section of
[ô] the WIN.INI file for the DEVICE= item
r = WriteProfileString([Ô]windows[Ô], [Ô]Device[Ô], DeviceLine)
[ô] Cause all applications to reload the INI file:
l = SendMessage(HWND_BROADCAST, WM_WININICHANGE1, 0, [Ô]windows[Ô])
End Sub
Private Sub GetDriverAndPort(ByVal Buffer As String, DriverName As _
String, PrinterPort As String)
Dim iDriver As Integer
Dim iPort As Integer
DriverName = [Ô][Ô]
PrinterPort = [Ô][Ô]
[ô] The driver name is first in the string terminated by a comma
iDriver = InStr(Buffer, [Ô],[Ô])
If iDriver > 0 Then
[ô] Strip out the driver name
DriverName = Left(Buffer, iDriver - 1)
[ô] The port name is the second entry after the driver name
[ô] separated by commas.
iPort = InStr(iDriver + 1, Buffer, [Ô],[Ô])
If iPort > 0 Then
[ô] Strip out the port name
PrinterPort = Mid(Buffer, iDriver + 1, _
iPort - iDriver - 1)
End If
End If
End Sub
Private Sub ParseList(lstCtl As Control, ByVal Buffer As String)
Dim i As Integer
Dim s As String
Do
i = InStr(Buffer, Chr(0))
If i > 0 Then
s = Left(Buffer, i - 1)
If Len(Trim(s)) Then lstCtl.AddItem s
Buffer = Mid(Buffer, i + 1)
Else
If Len(Trim(Buffer)) Then lstCtl.AddItem Buffer
Buffer = [Ô][Ô]
End If
Loop While i > 0
End Sub
[ô]se for para selecionar impressoras no sistema operacional XP
Private Sub WinNTSetDefaultPrinter()
Dim Buffer As String
Dim DeviceName As String
Dim DriverName As String
Dim PrinterPort As String
Dim PrinterName As String
Dim r As Long
If combo_impressoras <> Empty Then
[ô] Get the printer information for the currently selected
[ô] printer in the list. The information is taken from the
[ô] WIN.INI file.
Buffer = Space(1024)
PrinterName = combo_impressoras.Text
r = GetProfileString([Ô]PrinterPorts[Ô], PrinterName, [Ô][Ô], _
Buffer, Len(Buffer))
[ô] Parse the driver name and port name out of the buffer
GetDriverAndPort Buffer, DriverName, PrinterPort
If DriverName <> [Ô][Ô] And PrinterPort <> [Ô][Ô] Then
SetDefaultPrinter combo_impressoras.Text, DriverName, PrinterPort
If Printer.DeviceName <> combo_impressoras.Text Then
[ô] Make sure Printer object is set to the new printer
SelectPrinter (combo_impressoras.Text)
End If
End If
End If
End Sub
Citação::
Private Sub Form_Load()
Dim r As Long
Dim Buffer As String
[ô] Get the list of available printers from WIN.INI
Buffer = Space(8192)
r = GetProfileString([Ô]PrinterPorts[Ô], vbNullString, [Ô][Ô], _
Buffer, Len(Buffer))
[ô] Display the list of printer in the ListBox List1
ParseList combo_impressoras, Buffer
[ô]informa a impressora atual selecionada
atual.Caption = Printer.DeviceName
End Sub
Private Sub combo_impressoras_GotFocus()
combo_impressoras.SelStart = 0
combo_impressoras.BackColor = &HFFFF&
End Sub
Private Sub combo_impressoras_KeyDown(KeyCode As Integer, Shift As Integer)
If PosicaoDoCombo = 0 Then If KeyCode = 40 Then SendKeys [Ô]{F4}[Ô]: PosicaoDoCombo = 1
End Sub
Private Sub combo_impressoras_KeyPress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub
Private Sub combo_impressoras_LostFocus()
combo_impressoras.BackColor = &H80000005
End Sub
Private Sub Command1_Click()
If combo_impressoras.Text = Empty Then
MsgBox [Ô]Selecione uma opção de impressora na lista ...[Ô], vbExclamation
combo_impressoras.SetFocus
Else
DoEvents
Me.MousePointer = 11
DoEvents
Dim osinfo As OSVERSIONINFO
Dim retValue As Integer
osinfo.dwOSVersionInfoSize = 148
osinfo.szCSDVersion = Space$(128)
retValue = GetVersionExA(osinfo)
Call WinNTSetDefaultPrinter
atual.Caption = Printer.DeviceName
atual.BackColor = &HFFFF&
info01.Caption = [Ô]Nova impressora :[Ô]
info01.BackColor = &HFFFF&
DoEvents
Me.MousePointer = 0
End If
End Sub
[ô]definição da impressora do sistema
Private Sub SetDefaultPrinter(ByVal PrinterName As String, _
ByVal DriverName As String, ByVal PrinterPort As String)
Dim DeviceLine As String
Dim r As Long
Dim l As Long
DeviceLine = PrinterName & [Ô],[Ô] & DriverName & [Ô],[Ô] & PrinterPort
[ô] Store the new printer information in the [WINDOWS] section of
[ô] the WIN.INI file for the DEVICE= item
r = WriteProfileString([Ô]windows[Ô], [Ô]Device[Ô], DeviceLine)
[ô] Cause all applications to reload the INI file:
l = SendMessage(HWND_BROADCAST, WM_WININICHANGE1, 0, [Ô]windows[Ô])
End Sub
Private Sub GetDriverAndPort(ByVal Buffer As String, DriverName As _
String, PrinterPort As String)
Dim iDriver As Integer
Dim iPort As Integer
DriverName = [Ô][Ô]
PrinterPort = [Ô][Ô]
[ô] The driver name is first in the string terminated by a comma
iDriver = InStr(Buffer, [Ô],[Ô])
If iDriver > 0 Then
[ô] Strip out the driver name
DriverName = Left(Buffer, iDriver - 1)
[ô] The port name is the second entry after the driver name
[ô] separated by commas.
iPort = InStr(iDriver + 1, Buffer, [Ô],[Ô])
If iPort > 0 Then
[ô] Strip out the port name
PrinterPort = Mid(Buffer, iDriver + 1, _
iPort - iDriver - 1)
End If
End If
End Sub
Private Sub ParseList(lstCtl As Control, ByVal Buffer As String)
Dim i As Integer
Dim s As String
Do
i = InStr(Buffer, Chr(0))
If i > 0 Then
s = Left(Buffer, i - 1)
If Len(Trim(s)) Then lstCtl.AddItem s
Buffer = Mid(Buffer, i + 1)
Else
If Len(Trim(Buffer)) Then lstCtl.AddItem Buffer
Buffer = [Ô][Ô]
End If
Loop While i > 0
End Sub
[ô]se for para selecionar impressoras no sistema operacional XP
Private Sub WinNTSetDefaultPrinter()
Dim Buffer As String
Dim DeviceName As String
Dim DriverName As String
Dim PrinterPort As String
Dim PrinterName As String
Dim r As Long
If combo_impressoras <> Empty Then
[ô] Get the printer information for the currently selected
[ô] printer in the list. The information is taken from the
[ô] WIN.INI file.
Buffer = Space(1024)
PrinterName = combo_impressoras.Text
r = GetProfileString([Ô]PrinterPorts[Ô], PrinterName, [Ô][Ô], _
Buffer, Len(Buffer))
[ô] Parse the driver name and port name out of the buffer
GetDriverAndPort Buffer, DriverName, PrinterPort
If DriverName <> [Ô][Ô] And PrinterPort <> [Ô][Ô] Then
SetDefaultPrinter combo_impressoras.Text, DriverName, PrinterPort
If Printer.DeviceName <> combo_impressoras.Text Then
[ô] Make sure Printer object is set to the new printer
SelectPrinter (combo_impressoras.Text)
End If
End If
End If
End Sub
FABRICIOWEB, peguei só a parte Printer.DeviceName que pra mim já serviu para o que eu queria.
Vlw
Até mais,