打印机颜色模式
常数 值 描述
vbPRCMMonochrome 1 单色输出
vbPRCMColor 2 彩色输出
双面打印
常数 值 描述
vbPRDPSimplex 1 单面打印
vbPRDPHorizontal 2 双面水平打印
vbPRDPVertical 3 双面垂直打印
打印方向
常数 值 描述
vbPRORPortrait 1 文档打印以纸的窄边作顶部
vbPRORLandscape 2 文档打印以纸的宽边作顶部
打印质量
常数 值 描述
vbPRPQDraft -1 草稿打印质量
vbPRPQLow -2 低级打印质量
vbPRPQMedium -3 中等打印质量
vbPRPQHigh -4 高级打印质量
PaperBin 属性
常数 值 描述
vbPRBNUpper 1 从上层纸盒进纸
vbPRBNLower 2 从下层纸盒进纸
vbPRBNMiddle 3 从中间纸盒进纸
vbPRBNManual 4 等待手动插入每页纸
vbPRBNEnvelope 5 从信封进纸器进纸
vbPRBNEnvManual 6 从信封进纸器进纸;但要等待手动插入
vbPRBNAuto 7 (缺省方式)从当前缺省纸盒进纸
vbPRBNTractor 8 从拖拉进纸器供纸
vbPRBNSmallFmt 9 从小型进纸器进纸
vbPRBNLargeFmt 10 从大型纸盒进纸
vbPRBNLargeCapacity 11 从大容量进纸器进纸
vbPRBNCassette 14 从附加的卡式纸盒进纸
PaperSize 属性
常数 值 描述
vbPRPSLetter 1 信笺, 8 1/2 x 11 英寸
vbPRPSLetterSmall 2 +A611 小型信笺, 8 1/2 x 11 英寸
vbPRPSTabloid 3 小型报, 11 x 17 英寸
vbPRPSLedger 4 分类帐, 17 x 11 英寸
vbPRPSLegal 5 法律文件, 8 1/2 x 14 英寸
vbPRPSStatement 6 声明书,5 1/2 x 8 1/2 英寸
vbPRPSExecutive 7 行政文件,7 1/2 x 10 1/2 英寸
vbPRPSA3 8 A3, 297 x 420 mm
vbPRPSA4 9 A4, 210 x 297 mm
vbPRPSA4Small 10 A4小号, 210 x 297 mm
vbPRPSA5 11 A5, 148 x 210 mm
vbPRPSB4 12 B4, 250 x 354 mm
vbPRPSB5 13 B5, 182 x 257 mm
vbPRPSFolio 14 对开本, 8 1/2 x 13 英寸
vbPRPSQuarto 15 四开本, 215 x 275 mm
vbPRPS1&H14 16 10 x 14 英寸
vbPRPS11x17 17 11 x 17 英寸
vbPRPSNote 18 便条,8 1/2 x 11 英寸
vbPRPSEnv9 19 #9 信封, 3 7/8 x 8 7/8 英寸
vbPRPSEnv10 20 #10 信封, 4 1/8 x 9 1/2 英寸
vbPRPSEnv11 21 #11 信封, 4 1/2 x 10 3/8 英寸
vbPRPSEnv12 22 #12 信封, 4 1/2 x 11 英寸
vbPRPSEnv14 23 #14 信封, 5 x 11 1/2 英寸
vbPRPSCSheet 24 C 尺寸工作单
vbPRPSDSheet 25 D 尺寸工作单
vbPRPSESheet 26 E 尺寸工作单
vbPRPSEnvDL 27 DL 型信封, 110 x 220 mm
vbPRPSEnvC3 29 C3 型信封, 324 x 458 mm
vbPRPSEnvC4 30 C4 型信封, 229 x 324 mm
vbPRPSEnvC5 28 C5 型信封, 162 x 229 mm
vbPRPSEnvC6 31 C6 型信封, 114 x 162 mm
vbPRPSEnvC65 32 C65 型信封,114 x 229 mm
vbPRPSEnvB4 33 B4 型信封, 250 x 353 mm
vbPRPSEnvB5 34 B5 型信封,176 x 250 mm
vbPRPSEnvB6 35 B6 型信封, 176 x 125 mm
vbPRPSEnvItaly 36 信封, 110 x 230 mm
vbPRPSEnvMonarch 37 信封大王, 3 7/8 x 7 1/2 英寸
vbPRPSEnvPersonal 38 信封, 3 5/8 x 6 1/2 英寸
vbPRPSFanfoldUS 39 U.S. 标准复写簿, 14 7/8 x 11 英寸
vbPRPSFanfoldStdGerman 40 德国标准复写簿, 8 1/2 x 12 英寸
vbPRPSFanfoldLglGerman 41 德国法律复写簿, 8 1/2 x 13 英寸
vbPRPSUser 256 用户定义
----------------------------------------------------------------------------------------------------
EndDoc方法示例
本示例使用 EndDoc 方法在打印完两页之后结束一个文件,该被打印的每页按正文行居中方式显示页号。要检验此示例,可将本例代码粘贴到一个窗体的声明部分,然后按 F5 键并单击该窗体。
Private Sub Form_Click ()
Dim HWidth, HHeight, I, Msg ' 声明变量。
On Error GoTo ErrorHandler ' 设置错误处理程序。
Msg = "This is printed on page"
For I = 1 To 2 ' 设置 2 个迭代。
HWidth = Printer.TextWidth(Msg) / 2 ' 取得半宽。
HHeight = Printer.TextHeight(Msg) /2 ' 取得半高。
Printer.CurrentX = Printer.ScaleWidth / 2 - HWidth
Printer.CurrentY = Printer.ScaleHeight / 2 - HHeight
Printer.Print Msg & Printer.Page & "." ' 打印。
Printer.NewPage ' 发送新页。
Next I
Printer.EndDoc ' 打印完成。
Msg = "Two pages, each with a single, centered line of text, "
Msg = Msg & "have been sent to your printer."
MsgBox Msg ' 显示信息。
Exit Sub
ErrorHandler:
MsgBox "There was a problem printing to your printer."
Exit Sub
End Sub
KillDoc 方法示例
本示例使用 KillDoc 方法来终止当前打印工作。要检验此示例,可将本例代码粘贴到一个窗体的声明部分,然后按 F5 键并单击该窗体。
Private Sub Form_Click()
For i = 1 To 40
Printer.CurrentX = 1440 ' 设置左边距。
Printer.CurrentY = (i * 300) ' 进页到下一行。
Printer.Print "This is line" & Str$(i) & " of text."
On Error Resume Next ' 捕获任何打印机错误。
If i = 26 Then
Printer.KillDoc ' 立即终止打印作业。
Printer.EndDoc
End
End If
Next i
End Sub
NewPage 方法示例
本示例使用 NewPage 方法在一页上打印出一行居中正文后开始一个新的打印页。要检验此示例,可将本例代码粘贴到一个窗体的声明部分,然后按 F5 键并单击该窗体。
Private Sub Form_Click ()
Dim HWidth, HHeight, I, Msg ' 声明变量。
On Error GoTo ErrorHandler ' 设置错误处理程序。
Msg = "This is printed on page"
For I = 1 To 2 ' 设置 2 个迭代。
HWidth = Printer.TextWidth(Msg) / 2 ' 取半宽。
HHeight = Printer.TextHeight(Msg) /2 ' 取半高。
Printer.CurrentX = Printer.ScaleWidth / 2 - HWidth
Printer.CurrentY = Printer.ScaleHeight / 2 - HHeight
Printer.Print Msg & Printer.Page & "." ' 打印。
Printer.NewPage ' 发送新页。
Next I
Printer.EndDoc ' 打印完毕。
Msg = "Two pages, each with a single, centered line of text, "
Msg = Msg & "have been sent to your printer."
MsgBox Msg ' 显示信息。
Exit Sub
ErrorHandler:
MsgBox "There was a problem printing to your printer."
Exit Sub
End Sub
给你一段程序,作为一个模块,调用就可以了
Option Explicit
Public Enum PrinterOrientationConstants
OrientPortrait = 1
OrientLandscape = 2
End Enum
Private Type DEVMODE
dmDeviceName As String * 32
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 * 32
dmUnusedPadding As Integer
dmBitsPerPel As Integer
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type
Private Type PRINTER_DEFAULTS
pDataType As String
pDevMode As Long
DesiredAccess As Long
End Type
Private Type PRINTER_INFO_2
pServerName As Long
pPrinterName As Long
pShareName As Long
pPortName As Long
pDriverName As Long
pComment As Long
pLocation As Long
pDevMode As Long
pSepFile As Long
pPrintProcessor As Long
pDataType As Long
pParameters As Long
pSecurityDescriptor As Long
Attributes As Long
Priority As Long
DefaultPriority As Long
StartTime As Long
UntilTime As Long
Status As Long
cJobs As Long
AveragePPM As Long
End Type
'*******************'
' iDmpaper值 纸张 '
' 11 A5 '
' 13 B5 '
'*******************'
Private Const DM_IN_BUFFER As Long = 8
Private Const DM_OUT_BUFFER As Long = 2
Private Const DM_ORIENTATION As Long = &H1
Private Const DM_PAPERSIZE = &H2&
Private Const PRINTER_ACCESS_ADMINISTER As Long = &H4
Private Const PRINTER_ACCESS_USE As Long = &H8
Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Private Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED _
Or _
PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)
Private Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" _
(hpvDest As Any, hpvSource As Any, ByVal _
cbCopy As Long)
Private Declare Function OpenPrinter Lib "winspool.drv" Alias _
"OpenPrinterA" (ByVal pPrinterName As String, phPrinter As _
Long, pDefault As Any) 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, pDevModeOutput As _
Any, _
pDevModeInput As Any, _
ByVal fMode 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 SetPrinter Lib "winspool.drv" _
Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal level As Long, _
pPrinter As Any, ByVal Command As Long) As Long
'在打印或预览之前直接调用SetDefaultPrinterOrientation 打印走向常数。注意:红色注释部分用于改变纸张的大小。
Function SetDefaultPrinterOrientation(ByVal eOrientation As _
PrinterOrientationConstants, iDmpaper As Integer) As Boolean
Dim bDevMode() As Byte
Dim bPrinterInfo2() As Byte
Dim hPrinter As Long
Dim lResult As Long
Dim nSize As Long
Dim sPrnName As String
Dim dm As DEVMODE
Dim olddm As DEVMODE
Dim pd As PRINTER_DEFAULTS
Dim pi2 As PRINTER_INFO_2
' 获取默认打印机的设备名称
sPrnName = Printer.DeviceName
' 由于要调用SetPrinter,所以
' 如果是在NT下就要求PRINTER_ALL_ACCESS
pd.DesiredAccess = PRINTER_ALL_ACCESS
' 获取打印机句柄
If OpenPrinter(sPrnName, hPrinter, pd) Then
' 获取PRINTER_INFO_2结构要求的字节数
Call GetPrinter(hPrinter, 2&, 0&, 0&, nSize)
ReDim bPrinterInfo2(1 To nSize) As Byte
lResult = GetPrinter(hPrinter, 2, bPrinterInfo2(1), nSize, nSize)
Call CopyMemory(pi2, bPrinterInfo2(1), Len(pi2))
nSize = DocumentProperties(0&, hPrinter, sPrnName, 0&, 0&, 0)
ReDim bDevMode(1 To nSize)
If pi2.pDevMode Then
Call CopyMemory(bDevMode(1), ByVal pi2.pDevMode, Len(dm))
Else
Call DocumentProperties(0&, hPrinter, sPrnName, bDevMode(1), 0&, DM_OUT_BUFFER)
End If
Call CopyMemory(dm, bDevMode(1), Len(dm))
Call CopyMemory(olddm, bDevMode(1), Len(olddm))
With dm
' 设置新的走向
.dmOrientation = eOrientation
.dmFields = DM_ORIENTATION
.dmPaperSize = iDmpaper '将纸张大小设为iDmpaper,请自行更改所需大小
' .dmPaperLength = iDmpaperLength
' .dmPaperWidth = iDmpaperWidth
.dmFields = DM_PAPERSIZE '必须,否则无法设置纸张大小
End With
Call CopyMemory(bDevMode(1), dm, Len(dm))
Call DocumentProperties(0&, hPrinter, sPrnName, _
bDevMode(1), bDevMode(1), DM_IN_BUFFER Or _
DM_OUT_BUFFER)
pi2.pDevMode = VarPtr(bDevMode(1))
lResult = SetPrinter(hPrinter, 2, pi2, 0&)
Call ClosePrinter(hPrinter)
SetDefaultPrinterOrientation = True
Else
SetDefaultPrinterOrientation = False
End If
End Function