如何使报表输出到指定的打印机

seeQe 2010-09-01 02:29:54
用VB 中的DataReport 设计报表,要求输出到指定的打印机PrinterB,而不是默认打印机PrinterA,
这该如何做呢?

比如:

Load rptMeal
With rptMeal

.Refresh
.Show 1
'或者不显示
.Printreport False

End With

上述代码是自动选择的是默认打印机输出PrinterA 打印, 在显示框中可以选择打印机(速度慢),隐式模式则直接打印至默认打印机, 有没有方法指定输出的其他打印机PrinterB 呢?

thanks...
...全文
119 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
seeQe 2010-09-02
  • 打赏
  • 举报
回复
这个问题看似简单,是啊,好像回答的很少哦
lsh6688 2010-09-01
  • 打赏
  • 举报
回复
Option Explicit

Private Declare Function GetProfileString Lib "kernel32" _
Alias "GetProfileStringA" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long) As Long

Private Declare Function WriteProfileString Lib "kernel32" _
Alias "WriteProfileStringA" _
(ByVal lpszSection As String, _
ByVal lpszKeyName As String, _
ByVal lpszString As String) As Long

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lparam As String) As Long

Private Const HWND_BROADCAST = &HFFFF
Private Const WM_WININICHANGE = &H1A

Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type

Private Declare Function GetVersionExA Lib "kernel32" _
(lpVersionInformation As OSVERSIONINFO) As Integer

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

Private Declare Function ClosePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long

Private Declare Function lstrcpy Lib "kernel32" _
Alias "lstrcpyA" _
(ByVal lpString1 As String, _
ByVal lpString2 As Any) As Long

'DEVMODE结构常量
Private Const CCHDEVICENAME = 32
Private Const CCHFORMNAME = 32

'获取 PRINTER_DEFAULTS权限常量
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const PRINTER_ACCESS_ADMINISTER = &H4
Private Const PRINTER_ACCESS_USE = &H8
Private Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)

'PRINTER_INFO_5 Attributes 成员设置这个
Private Const PRINTER_ATTRIBUTE_DEFAULT = 4

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
dmLogPixels As Integer
dmBitsPerPel As Long
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
dmICMMethod As Long 'Windows 95 only
dmICMIntent As Long 'Windows 95 only
dmMediaType As Long 'Windows 95 only
dmDitherType As Long 'Windows 95 only
dmReserved1 As Long 'Windows 95 only
dmReserved2 As Long 'Windows 95 only
End Type

Private Type PRINTER_INFO_5
pPrinterName As String
pPortName As String
Attributes As Long
DeviceNotSelectedTimeout As Long
TransmissionRetryTimeout As Long
End Type

'字段 类型及说明
'pDatatype String,打印机使用的默认数据类型的名字。可设为vbNullString,表示使用当前的默认配置
'pDevMode Long,打印机默认配置信息的一个地址。可设为零,表示使用当前默认设置
'DesiredAccess Long,PRINTER_ACCESS_ADMINISTER 或 PRINTER_ACCESS_USE,或者是一个常规的安全值(取决于您是需要配置打印机,还是仅仅访问它)
Private Type PRINTER_DEFAULTS
pDatatype As Long
pDevMode As DEVMODE
DesiredAccess As Long
End Type

Private m_sCurrPrinterDevName As String
Private m_sPrevPrinterDevName As String
Private m_sPrevPrinterDriver As String
Private m_sPrevPrinterPort As String

Private Sub Class_Initialize()

Dim Buffer As String
Dim r As Long

Buffer = Space$(8192)
r = GetProfileString("windows", "Device", "", Buffer, Len(Buffer))

If r Then

Buffer = Mid$(Buffer, 1, r)

'存储当前的打印机数据在修改之前
m_sPrevPrinterDevName = Mid$(Buffer, 1, InStr(Buffer, ",") - 1)
m_sPrevPrinterDriver = Mid$(Buffer, InStr(Buffer, ",") + 1, InStrRev(Buffer, ",") - InStr(Buffer, ",") - 1)
m_sPrevPrinterPort = Mid$(Buffer, InStrRev(Buffer, ",") + 1)

Else

m_sPrevPrinterDevName = ""
m_sPrevPrinterDriver = ""
m_sPrevPrinterDevName = ""

End If

m_sCurrPrinterDevName = m_sPrevPrinterDevName

End Sub

Private Function SetDefaultPrinter(ByVal DeviceName As String, ByVal DriverName As String, ByVal PrinterPort As String) As Boolean

Dim DeviceLine As String
Dim r As Long
Dim l As Long

DeviceLine = DeviceName & "," & DriverName & "," & PrinterPort

r = WriteProfileString("windows", "Device", DeviceLine)

If r Then

'向系统广播修改注册表
l = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows")
SetDefaultPrinter = True
m_sCurrPrinterDevName = DeviceName

Else

SetDefaultPrinter = False

End If

End Function

Private Function Win95SetDefaultPrinter(ByRef DeviceName As String) As Boolean

Dim lngPhandle As Long
Dim PD As PRINTER_DEFAULTS
Dim lngRet As Long
Dim lngNeedByte As Long
Dim PI5 As PRINTER_INFO_5

If DeviceName = "" Then

Win95SetDefaultPrinter = False
Exit Function

End If

'填充PRINTER_DEFAULTS成员
PD.pDatatype = 0&
PD.DesiredAccess = PRINTER_ALL_ACCESS

'获取打印机句柄
lngRet = OpenPrinter(DeviceName, lngPhandle, PD)

If lngRet = False Then

Win95SetDefaultPrinter = False
Exit Function

End If

'开始调用GetPrinter, 要求5级PRINTER_INFO_5信息
'先确定需要多少字节数
lngRet = GetPrinter(lngPhandle, 5, ByVal 0&, 0, lngNeedByte)

ReDim t((lngNeedByte \ 4)) As Long

lngRet = GetPrinter(lngPhandle, 5, t(0), lngNeedByte, lngNeedByte)

If lngRet = False Then

Win95SetDefaultPrinter = False
Exit Function

End If

'填充PI5结构成员给SetPrinter.
'PtrCtoVbString复制内存指针给两个字符成员
'pointers contained in the t() array into a Visual Basic string.
'其余三个成分刚好是DWORDS (long integers)不需要任何转换
PI5.pPrinterName = PtrCtoVbString(t(0)) '得到打印机名
PI5.pPortName = PtrCtoVbString(t(1)) '得到端口
PI5.Attributes = t(2) '得到打印机属性值
PI5.DeviceNotSelectedTimeout = t(3) '得到没有选择打印机超时时间
PI5.TransmissionRetryTimeout = t(4) '得到重试传输超时时间

'结构设置默认打印机标志
PI5.Attributes = PRINTER_ATTRIBUTE_DEFAULT
'设置默认的打印机
lngRet = SetPrinter(lngPhandle, 5, PI5, 0) 'Command传0根据PRINTER_INFO_x结构改变打印机

If lngRet = False Then

Win95SetDefaultPrinter = False
Exit Function

End If

'关闭打印机句柄
Call ClosePrinter(lngPhandle)
m_sCurrPrinterDevName = DeviceName
Win95SetDefaultPrinter = True

End Function

Private Function PtrCtoVbString(Add As Long) As String

'因为VB不支持指针数据类型
'你不能直接从一个指针返回字符
'可以通过lstrcpy从指针获取字符
Dim sTemp As String * 512, x As Long

x = lstrcpy(sTemp, Add)

If (InStr(1, sTemp, Chr$(0)) = 0) Then

PtrCtoVbString = ""

Else

PtrCtoVbString = Left$(sTemp, InStr(1, sTemp, Chr$(0)) - 1)

End If

End Function
lsh6688 2010-09-01
  • 打赏
  • 举报
回复
这个你需要在计算的大打印机设置里面 设置默认打印机就好了

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧