先判断打印位置,如果超出边界利用Printer.newpage,代码如下: '定RS为你的记录集 dim bottom as single bottom=printer.scaletop+printer.scaleheight-1440'设置纸张的下边界 rs.movefirst printer.currentY=1400 do while not rs.eof printer.currentX=1400 printer.print "aa"'打印内容 if printer.currentY>=bottom then printer.newpage printer.currentY=1400 end if rs.movenext loop rs.close printer.enddoc
抱歉,那个控建我不熟悉你可以看看API,不知道队你有没有用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 EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, pJob As Any, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long Private Sub Form_Load() 'KPD-Team 2001 'URL: http://www.allapi.net/ 'E-Mail: [email protected] Dim hPrinter As Long, lNeeded As Long, lReturned As Long Dim lJobCount As Long OpenPrinter Printer.DeviceName, hPrinter, ByVal 0& EnumJobs hPrinter, 0, 99, 1, ByVal 0&, 0, lNeeded, lReturned If lNeeded > 0 Then ReDim byteJobsBuffer(lNeeded - 1) As Byte EnumJobs hPrinter, 0, 99, 1, byteJobsBuffer(0), lNeeded, lNeeded, lReturned If lReturned > 0 Then lJobCount = lReturned Else lJobCount = 0 End If Else lJobCount = 0 End If ClosePrinter hPrinter MsgBox "Jobs in printer queue: " + CStr(lJobCount), vbInformation End Sub
' Get information about all of the local printers using structure 1. Note how ' the elements of the array are loaded into an array of data structures manually. Also ' note how the following special declares must be used to allow numeric string pointers ' to be used in place of strings: Private Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long Private Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Long) As Long Private Declare Function EnumPrinters Lib "winspool.drv" Alias "EnumPrintersA" (ByVal flags As Long, ByVal name As String, ByVal Level As Long, pPrinterEnum As Long, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long Const PRINTER_ENUM_LOCAL = &H2 Private Type PRINTER_INFO_1 flags As Long pDescription As String pName As String pComment As String End Type Private Sub Form_Load() 'KPD-Team 1999 'URL: http://www.allapi.net/ 'E-Mail: [email protected] Dim longbuffer() As Long ' resizable array receives information from the function Dim printinfo() As PRINTER_INFO_1 ' values inside longbuffer() will be put into here Dim numbytes As Long ' size in bytes of longbuffer() Dim numneeded As Long ' receives number of bytes necessary if longbuffer() is too small Dim numprinters As Long ' receives number of printers found Dim c As Integer, retval As Long ' counter variable & return value Me.AutoRedraw = True 'Set current graphic mode to persistent ' Get information about the local printers numbytes = 3076 ' should be sufficiently big, but it may not be ReDim longbuffer(0 To numbytes / 4) As Long ' resize array -- note how 1 Long = 4 bytes retval = EnumPrinters(PRINTER_ENUM_LOCAL, "", 1, longbuffer(0), numbytes, numneeded, numprinters) If retval = 0 Then ' try enlarging longbuffer() to receive all necessary information numbytes = numneeded ReDim longbuffer(0 To numbytes / 4) As Long ' make it large enough retval = EnumPrinters(PRINTER_ENUM_LOCAL, "", 1, longbuffer(0), numbytes, numneeded, numprinters) If retval = 0 Then ' failed again! Debug.Print "Could not successfully enumerate the printes." End ' abort program End If End If ' Convert longbuffer() data into printinfo() If numprinters <> 0 Then ReDim printinfo(0 To numprinters - 1) As PRINTER_INFO_1 ' room for each printer For c = 0 To numprinters - 1 ' loop, putting each set of information into each element ' longbuffer(4 * c) = .flags, longbuffer(4 * c + 1) = .pDescription, etc. ' For each string, the string is first buffered to provide enough room, and then the string is copied. printinfo(c).flags = longbuffer(4 * c) printinfo(c).pDescription = Space(lstrlen(longbuffer(4 * c + 1))) retval = lstrcpy(printinfo(c).pDescription, longbuffer(4 * c + 1)) printinfo(c).pName = Space(lstrlen(longbuffer(4 * c + 2))) retval = lstrcpy(printinfo(c).pName, longbuffer(4 * c + 2)) printinfo(c).pComment = Space(lstrlen(longbuffer(4 * c + 3))) retval = lstrcpy(printinfo(c).pComment, longbuffer(4 * c + 3)) Next c ' Display name of each printer For c = 0 To numprinters - 1 Me.Print "Name of printer"; c + 1; " is: "; printinfo(c).pName Next c End Sub
----------------------------------------------------- 'Code generously provided by Merrion Computing 'Visit their website at http://www.merrioncomputing.com/ 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 Private Sub Form_Load() Dim SizeNeeded As Long, buffer() As Long Dim pDef As PRINTER_DEFAULTS 'Get a handle to the printer lret = OpenPrinter(Printer.DeviceName, 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 PrintData "Server name", StringFromPointer(buffer(0), 255) PrintData "Printer name", StringFromPointer(buffer(1), 255) PrintData "Share name", StringFromPointer(buffer(2), 255) PrintData "Port name", StringFromPointer(buffer(3), 255) PrintData "Driver name", StringFromPointer(buffer(4), 255) PrintData "Comment", StringFromPointer(buffer(5), 255) PrintData "Location", StringFromPointer(buffer(6), 255) Unload Me End Sub Sub PrintData(Name As String, Data As String) If LenB(Data) > 0 Then Debug.Print Name + ": " + Data End If End Sub
这是2.0版的。Description Returns the current print job status. Data Type JobStatus Settings Value Mnemonic Description 0 ddJSIdle Idle 1 ddJSPrinting Printing 2 ddJSCompleted Completed 3 ddJSAborted Aborted Availability Design time N/A Run time Read Only Example lblStatus.Caption = rptInvoice.Printer.Status
你可以: rpt.Printer.StartJob "One Page" rpt.Printer.StartPage ... do while rpt.Printer.state<>2 doevents' wait to print loop rpt.Printer.EndPage rpt.Printer.EndJob
to cslf(cs) 可我不管么使,rpt.Printer.state 都为2 !!!! 是不是有BUG呀??to nik_Amis(Azrael) 你的方法好使。 又有个新问题: 只要一开始打印票据我就开始判断打印任务是否结束,如果结束了就开始切纸,但现在的问题就是:从开始打印到切纸的这个过程中不能执行其它的操作。可不可以把判断打印任务的过程做成一多线程的?或是通过什么好的方法打印机打完了,自动的切纸呢?????
没有使用过pos系统切纸的命令是否同样缓冲在了printer的指令里面的哦!!
'定RS为你的记录集
dim bottom as single
bottom=printer.scaletop+printer.scaleheight-1440'设置纸张的下边界
rs.movefirst
printer.currentY=1400
do while not rs.eof
printer.currentX=1400
printer.print "aa"'打印内容
if printer.currentY>=bottom then
printer.newpage
printer.currentY=1400
end if
rs.movenext
loop
rs.close
printer.enddoc
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, pJob As Any, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
Private Sub Form_Load()
'KPD-Team 2001
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim hPrinter As Long, lNeeded As Long, lReturned As Long
Dim lJobCount As Long
OpenPrinter Printer.DeviceName, hPrinter, ByVal 0&
EnumJobs hPrinter, 0, 99, 1, ByVal 0&, 0, lNeeded, lReturned
If lNeeded > 0 Then
ReDim byteJobsBuffer(lNeeded - 1) As Byte
EnumJobs hPrinter, 0, 99, 1, byteJobsBuffer(0), lNeeded, lNeeded, lReturned
If lReturned > 0 Then
lJobCount = lReturned
Else
lJobCount = 0
End If
Else
lJobCount = 0
End If
ClosePrinter hPrinter
MsgBox "Jobs in printer queue: " + CStr(lJobCount), vbInformation
End Sub
' the elements of the array are loaded into an array of data structures manually. Also
' note how the following special declares must be used to allow numeric string pointers
' to be used in place of strings:
Private Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Private Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Long) As Long
Private Declare Function EnumPrinters Lib "winspool.drv" Alias "EnumPrintersA" (ByVal flags As Long, ByVal name As String, ByVal Level As Long, pPrinterEnum As Long, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
Const PRINTER_ENUM_LOCAL = &H2
Private Type PRINTER_INFO_1
flags As Long
pDescription As String
pName As String
pComment As String
End Type
Private Sub Form_Load()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim longbuffer() As Long ' resizable array receives information from the function
Dim printinfo() As PRINTER_INFO_1 ' values inside longbuffer() will be put into here
Dim numbytes As Long ' size in bytes of longbuffer()
Dim numneeded As Long ' receives number of bytes necessary if longbuffer() is too small
Dim numprinters As Long ' receives number of printers found
Dim c As Integer, retval As Long ' counter variable & return value
Me.AutoRedraw = True 'Set current graphic mode to persistent
' Get information about the local printers
numbytes = 3076 ' should be sufficiently big, but it may not be
ReDim longbuffer(0 To numbytes / 4) As Long ' resize array -- note how 1 Long = 4 bytes
retval = EnumPrinters(PRINTER_ENUM_LOCAL, "", 1, longbuffer(0), numbytes, numneeded, numprinters)
If retval = 0 Then ' try enlarging longbuffer() to receive all necessary information
numbytes = numneeded
ReDim longbuffer(0 To numbytes / 4) As Long ' make it large enough
retval = EnumPrinters(PRINTER_ENUM_LOCAL, "", 1, longbuffer(0), numbytes, numneeded, numprinters)
If retval = 0 Then ' failed again!
Debug.Print "Could not successfully enumerate the printes."
End ' abort program
End If
End If
' Convert longbuffer() data into printinfo()
If numprinters <> 0 Then ReDim printinfo(0 To numprinters - 1) As PRINTER_INFO_1 ' room for each printer
For c = 0 To numprinters - 1 ' loop, putting each set of information into each element
' longbuffer(4 * c) = .flags, longbuffer(4 * c + 1) = .pDescription, etc.
' For each string, the string is first buffered to provide enough room, and then the string is copied.
printinfo(c).flags = longbuffer(4 * c)
printinfo(c).pDescription = Space(lstrlen(longbuffer(4 * c + 1)))
retval = lstrcpy(printinfo(c).pDescription, longbuffer(4 * c + 1))
printinfo(c).pName = Space(lstrlen(longbuffer(4 * c + 2)))
retval = lstrcpy(printinfo(c).pName, longbuffer(4 * c + 2))
printinfo(c).pComment = Space(lstrlen(longbuffer(4 * c + 3)))
retval = lstrcpy(printinfo(c).pComment, longbuffer(4 * c + 3))
Next c
' Display name of each printer
For c = 0 To numprinters - 1
Me.Print "Name of printer"; c + 1; " is: "; printinfo(c).pName
Next c
End Sub
'Code generously provided by Merrion Computing
'Visit their website at http://www.merrioncomputing.com/
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
Private Sub Form_Load()
Dim SizeNeeded As Long, buffer() As Long
Dim pDef As PRINTER_DEFAULTS
'Get a handle to the printer
lret = OpenPrinter(Printer.DeviceName, 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
PrintData "Server name", StringFromPointer(buffer(0), 255)
PrintData "Printer name", StringFromPointer(buffer(1), 255)
PrintData "Share name", StringFromPointer(buffer(2), 255)
PrintData "Port name", StringFromPointer(buffer(3), 255)
PrintData "Driver name", StringFromPointer(buffer(4), 255)
PrintData "Comment", StringFromPointer(buffer(5), 255)
PrintData "Location", StringFromPointer(buffer(6), 255)
Unload Me
End Sub
Sub PrintData(Name As String, Data As String)
If LenB(Data) > 0 Then
Debug.Print Name + ": " + Data
End If
End Sub
Returns the current print job status. Data Type
JobStatus Settings
Value Mnemonic Description
0 ddJSIdle Idle
1 ddJSPrinting Printing
2 ddJSCompleted Completed
3 ddJSAborted Aborted Availability
Design time N/A
Run time Read Only Example
lblStatus.Caption = rptInvoice.Printer.Status
rpt.Printer.StartJob "One Page"
rpt.Printer.StartPage
...
do while rpt.Printer.state<>2
doevents' wait to print
loop
rpt.Printer.EndPage
rpt.Printer.EndJob
可我不管么使,rpt.Printer.state 都为2 !!!! 是不是有BUG呀??to nik_Amis(Azrael)
你的方法好使。
又有个新问题:
只要一开始打印票据我就开始判断打印任务是否结束,如果结束了就开始切纸,但现在的问题就是:从开始打印到切纸的这个过程中不能执行其它的操作。可不可以把判断打印任务的过程做成一多线程的?或是通过什么好的方法打印机打完了,自动的切纸呢?????