整理一下关于分栏及分页打印.
刚刚浏览了帖子,又看到有问这个问题的,所以发一个之前我写的分栏打印的代码供参考,希望对以后出现此类问题有所帮助:
(窗体上两个MSHFlexGrid,左右各一)
Option ExplicitPrivate RsPrint As ADODB.RecordsetPrivate CurPos As Long '游标当前记录的位置Private SumPagesMoney As CurrencyPrivate SumPagesCount As LongPublic Function DoModal(vRst As ADODB.Recordset)'记录源自己写好,传进来 Set RsPrint = vRst frmPrint.ShowEnd FunctionPrivate Sub Form_Load() With frmPrint .Appearance = 0 .AutoRedraw = 1 .BackColor = &H80000005 '背景颜色:白色 .BorderStyle = 0 .Height = 15000 '设置页面的大小(纸的大小) .Width = 11760 .Top = 0 .Left = 0 .ClipControls = 0 'False .FillStyle = 0 'Solid .Caption = "paper" End With Label5.Caption = Trim(custdaily.Text1.Text) Label10.Caption = Trim(custdaily.Text3.Text) Call InitMSFG(MSHFlexGrid1) Call InitMSFG(MSHFlexGrid2) RsPrint.PageSize = MSHFlexGrid1.Rows - 1 '设置记录集中记录页里记录条数,也即要显示在每栏上的记录条数 CurPos = 0 SumPagesMoney = 0 SumPagesCount = 0 GoPrint '打印...End SubPrivate Sub GoPrint() Dim rc As Integer, cc As Integer '一页一栏的行数rc,列数cc Dim prc As Integer '记录集内记录的总页数 Dim rctotal As Integer '总行数 Dim i As Integer Me.Height = 15000' Printer.ScaleMode = vbMillimeters If RsPrint.RecordCount > 0 Then prc = RsPrint.PageCount ' rctotal = RsPrint.RecordCount RsPrint.MoveLast: RsPrint.MoveFirst For i = 1 To RsPrint.RecordCount SumPagesMoney = SumPagesMoney + Val(RsPrint.Fields(1).Value)' SumPagesCount = SumPagesCount + Val(RsPrint.Fields(2).Value) RsPrint.MoveNext Next RsPrint.MoveLast: RsPrint.MoveFirst For i = 1 To prc Step 2 Call InitMSFG(MSHFlexGrid1) Call InputMSFG(i, RsPrint, MSHFlexGrid1) '第一栏内容 If i + 1 <= RsPrint.PageCount Then Call InitMSFG(MSHFlexGrid2) Call InputMSFG(i + 1, RsPrint, MSHFlexGrid2) '第二栏内容 Else MSHFlexGrid2.Visible = False MSHFlexGrid2.Clear End If '页脚处理 Call LoacalSum(rctotal, i \ 2 + 1, IIf(prc Mod 2 = 0, prc / 2, prc \ 2 + 1)) ' MsgBox ("打印第" & Fix(I / 2) + 1 & "页") Me.PrintForm '输出到系统缺省打印机 Next Else MsgBox "查无此记录" End If ' RsPrint.Close '关闭记录' Set RsPrint = Nothing '释放缓冲区End Sub Private Sub InitMSFG(vMSF As MSHFlexGrid) With vMSF .Clear .Top = 780 .Rows = 51 '设置一栏容纳的行数(包括列表头) .Cols = 4 .FixedCols = 0 .FixedRows = 0 .BackColorFixed = 255 .BackColorBkg = -2147483639 .GridColor = 8454016 .GridColorFixed = 8454143 .Gridlines = 1 .MergeCells = 4 .BorderStyle = 1 '设置边框:有边框 .Appearance = 0 .ColWidth(0) = 600 .ColWidth(1) = 2600 .ColWidth(2) = 1500 .ColWidth(3) = 800 .TextMatrix(0, 0) = "序号" .TextMatrix(0, 1) = "客户名称" .TextMatrix(0, 2) = "总计金额" .TextMatrix(0, 3) = "备注" .Width = 5500 '设置宽度容纳一栏所有的列。 End With MSHFlexGrid2.Left = MSHFlexGrid1.Left + MSHFlexGrid1.Width + 100End Sub'读取记录集中(vMSFG行数)的多条记录Private Sub InputMSFG(vIntPage As Integer, rstData As ADODB.Recordset, vMSFG As MSHFlexGrid) Dim i As Long, j As Long Dim mMoney As Currency, mCount As Long Dim mRowHeight As Integer mRowHeight = vMSFG.RowHeight(0) ' rstData.AbsolutePosition = (vIntPage - 1) * rstData.PageSize + 1'仅向前游标或动态游标无用 For i = 1 To vMSFG.Rows - 1 If CurPos < RsPrint.RecordCount Then CurPos = CurPos + 1 With vMSFG .Row = i .TextMatrix(i, 0) = CurPos .TextMatrix(i, 1) = rstData.Fields(0).Value & "" .TextMatrix(i, 2) = Format(rstData.Fields(1).Value & "", "0.00") End With mMoney = mMoney + Val(rstData.Fields(1).Value)' mCount = mCount + Val(rstData.Fields(2).Value) mRowHeight = mRowHeight + vMSFG.RowHeight(i) rstData.MoveNext End If Next With vMSFG If .Row < .Rows - 1 Then '若最后一页的记录不够填充整张表 .Rows = .Row + 1 End If .Rows = .Rows + 1 .TextMatrix(.Rows - 1, 0) = "小计" .TextMatrix(.Rows - 1, 1) = "" .TextMatrix(.Rows - 1, 2) = Format(mMoney, "0.00") .TextMatrix(.Rows - 1, 3) = "" .Height = mRowHeight + .RowHeight(.Rows - 1) '再设置一下表格显示高度 End WithEnd SubPrivate Sub LoacalSum(ByVal vIntI As Integer, ByVal vIntJ As Integer, ByVal vIntK As Integer) '下置一横线,写总计之类 Label8.Caption = "(" & "金额" & ")" & Format(Val(MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Rows - 1, 2)) + Val(MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Rows - 1, 2)), "0.00") Label3.Caption = "(" & "金额" & ")" & Format(SumPagesMoney, "0.00") Label6.Caption = "共" & vIntI & "条," & vIntJ & "/" & vIntK & "页" Line1.x1 = MSHFlexGrid1.Left Line1.X2 = MSHFlexGrid1.Left + MSHFlexGrid1.Width + 100 + MSHFlexGrid2.Width Line1.y1 = MSHFlexGrid1.Top + MSHFlexGrid1.Height + 300 Line1.Y2 = Line1.y1 Label7.Left = MSHFlexGrid1.Left: Label8.Left = Label7.Left + Label7.Width + 100 Label7.Top = Line1.y1 + 100: Label8.Top = Label7.Top Label2.Left = Label7.Left: Label3.Left = Label8.Left Label2.Top = Label7.Top + Label7.Height + 100: Label3.Top = Label2.Top Label6.Left = Me.ScaleWidth - Label6.Width - 200 Label6.Top = Me.ScaleHeight - Label6.Height - 200 Label10.Left = Me.ScaleWidth - Label10.Width - 600 Label9.Left = Label10.Left - Label9.Width - 50 Label5.Left = Label9.Left - Label5.Width - 50 Label4.Left = Label5.Left - Label4.Width - 50End Sub