如何为reportviewer指定打印机而不用系统默认的?

2024-11-25 19:37:53
推荐回答(1个)
回答1:

Public Type PrinterInfo
pServerName As String
pPrinterName As String
pShareName As String
pPortName As String
pDriverName As String
pComment As String
pLocation As String
End Type

Private Const CCHDEVICENAME = 32
Private Const CCHFORMNAME = 32
Private Type DEVMODE
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 Type PRINTER_INFO_2
pServerName As String
pPrinterName As String
pShareName As String
pPortName As String
pDriverName As String
pComment As String
pLocation As String
pDevMode As Long
pSepFile As String
pPrintProcessor As String
pDatatype As String
pParameters As String
pSecurityDescriptor As Long
Attributes As Long
Priority As Long
DefaultPriority As Long
StartTime As Long
UntilTime As Long
Status As Long
JobsCount As Long
AveragePPM As Long
End Type
Private Type PRINTER_DEFAULTS
pDatatype As String
pDevMode As DEVMODE
DesiredAccess As Long
End Type
Public Enum Printer_Status
PRINTER_STATUS_READY = &H0
PRINTER_STATUS_PAUSED = &H1
PRINTER_STATUS_ERROR = &H2
PRINTER_STATUS_PENDING_DELETION = &H4
PRINTER_STATUS_PAPER_JAM = &H8
PRINTER_STATUS_PAPER_OUT = &H10
PRINTER_STATUS_MANUAL_FEED = &H20
PRINTER_STATUS_PAPER_PROBLEM = &H40
PRINTER_STATUS_OFFLINE = &H80
PRINTER_STATUS_IO_ACTIVE = &H100
PRINTER_STATUS_BUSY = &H200
PRINTER_STATUS_PRINTING = &H400
PRINTER_STATUS_OUTPUT_BIN_FULL = &H800
PRINTER_STATUS_NOT_AVAILABLE = &H1000
PRINTER_STATUS_WAITING = &H2000
PRINTER_STATUS_PROCESSING = &H4000
PRINTER_STATUS_INITIALIZING = &H8000
PRINTER_STATUS_WARMING_UP = &H10000
PRINTER_STATUS_TONER_LOW = &H20000
PRINTER_STATUS_NO_TONER = &H40000
PRINTER_STATUS_PAGE_PUNT = &H80000
PRINTER_STATUS_USER_INTERVENTION = &H100000
PRINTER_STATUS_OUT_OF_MEMORY = &H200000
PRINTER_STATUS_DOOR_OPEN = &H400000
PRINTER_STATUS_SERVER_UNKNOWN = &H800000
PRINTER_STATUS_POWER_SAVE = &H1000000
End Enum
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 GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, buffer As Long, ByVal pbSize As Long, pbSizeNeeded As Long) As Long
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function IsBadStringPtrByLong Lib "kernel32" Alias "IsBadStringPtrA" (ByVal lpsz As Long, ByVal ucchMax As Long) As Long
Public Function StringFromPointer(lpString As Long, lMaxLength As Long) As String
Dim sRet As String
Dim lret As Long
If lpString = 0 Then
StringFromPointer = ""
Exit Function
End If
If IsBadStringPtrByLong(lpString, lMaxLength) Then
' An error has occured - do not attempt to use this pointer
StringFromPointer = ""
Exit Function
End If
' Pre-initialise the return string...
sRet = Space$(lMaxLength)
CopyMemory ByVal sRet, ByVal lpString, ByVal Len(sRet)
If Err.LastDllError = 0 Then
If InStr(sRet, Chr$(0)) > 0 Then
sRet = Left$(sRet, InStr(sRet, Chr$(0)) - 1)
End If
End If
StringFromPointer = sRet
End Function
Public Function GetPrinterInfo(ByVal PrinterDeviceName As String) As PrinterInfo
Dim SizeNeeded As Long, buffer() As Long
Dim pDef As PRINTER_DEFAULTS
Dim mhPrinter As Long
Dim lret As Long
Dim prtInfo As PrinterInfo
'Get a handle to the printer
lret = OpenPrinter(PrinterDeviceName, mhPrinter, pDef)
'Initialize the buffer
ReDim Preserve buffer(0 To 0) As Long
'Retrieve the required size (in bytes)
lret = GetPrinter(mhPrinter, 2, buffer(0), UBound(buffer), SizeNeeded)
'Resize the buffer... Note that a Long is four bytes
ReDim Preserve buffer(0 To (SizeNeeded / 4) + 3) As Long
'Retrieve the Printer information
lret = GetPrinter(mhPrinter, 2, buffer(0), UBound(buffer) * 4, SizeNeeded)
'The data stored in 'buffer' corresponds with the data of a PRINTER_INFO_2 structure
ClosePrinter mhPrinter
'Show the data
With prtInfo
.pServerName = StringFromPointer(buffer(0), 255)
.pPrinterName = StringFromPointer(buffer(1), 255)
.pShareName = StringFromPointer(buffer(2), 255)
.pPortName = StringFromPointer(buffer(3), 255)
.pDriverName = StringFromPointer(buffer(4), 255)
.pComment = StringFromPointer(buffer(5), 255)
.pLocation = StringFromPointer(buffer(6), 255)
End With
GetPrinterInfo = prtInfo
End Function

Option Explicit
Dim strConn As String
Dim Conn As ADODB.Connection
'Dim datCmd As ADODB.Command
Dim craxReport As CRAXDRT.Report
Dim craxApp As CRAXDRT.Application
Dim ParameterField As ParameterFieldDefinition
Dim ReportView As frmPreView
Dim mPrinterName As String
Dim mDriverName As String
Dim mPortName As String
Dim mServerName As String
Dim mDataBase As String
Dim mUserID As String
Dim mPassword As String
Public Sub PrintSideMark(ByVal m_Mo As String, m_ItemNo1 As String, m_ClassId As String, ByVal IsDirectPrint As Boolean)
'Dim i As Integer
Dim j As Integer
strConn = ""
If craxReport Is Nothing Then
Set craxReport = craxApp.OpenReport(App.Path & "/BarCode.rpt", 1)
strConn = "Provider=SQLOLEDB.1;Password=" & mPassword & ";Persist Security Info=True;User ID=" & mUserID & ";Initial Catalog=" & mDataBase & ";Data Source=" & mServerName
craxReport.DataBase.LogOnServerEx "crdb_ado.dll", mServerName, mDataBase, mUserID, mPassword, "OLE DB (ADO)", strConn
End If

If craxReport.HasSavedData Then
craxReport.DiscardSavedData
End If
With craxReport
For j = 1 To .ParameterFields.Count
.ParameterFields(j).ClearCurrentValueAndRange
Next j
End With

Set ParameterField = craxReport.ParameterFields.GetItemByName("@Mo")
ParameterField.AddCurrentValue (m_Mo)
craxReport.ParameterFields.GetItemByName("@ItemNo1").AddCurrentValue (m_ItemNo1)
craxReport.ParameterFields.GetItemByName("@ClassId").AddCurrentValue (m_ClassId)

With craxReport
'Debug.Print .PaperSize
'Debug.Print .PaperOrientation
.SelectPrinter mDriverName, mPrinterName, mPortName
.PaperSize = crPaperUser
.PaperOrientation = crPortrait
End With

If Not IsDirectPrint Then
LoadReport
Else
craxReport.PrintOut False
End If
End Sub
Private Sub LoadReport()
If ReportView Is Nothing Then
Set ReportView = New frmPreView
End If
Screen.MousePointer = vbHourglass
ReportView.CRViewer91.ReportSource = craxReport
ReportView.CRViewer91.ViewReport
ReportView.Show
Screen.MousePointer = vbDefault
End Sub
Public Property Let DriverName(ByVal vNewValue As Variant)
mDriverName = vNewValue
End Property
Public Property Let PrinterName(ByVal vNewValue As Variant)
mPrinterName = vNewValue
End Property
Public Property Let PortName(ByVal vNewValue As Variant)
mPortName = vNewValue
End Property
Private Sub Class_Initialize()
Set craxApp = New CRAXDRT.Application
End Sub
Public Property Let ServerName(ByVal vNewValue As Variant)
mServerName = vNewValue
End Property
Public Property Let DataBase(ByVal vNewValue As Variant)
mDataBase = vNewValue
End Property
Public Property Let UserID(ByVal vNewValue As Variant)
mUserID = vNewValue
End Property
Public Property Let Password(ByVal vNewValue As Variant)
mPassword = vNewValue
End Property