VB打印功能全代码

Private  Sub  Image5_Click()  '打印报表  
       Dim  i  As  Integer  
       Dim  pageleft  As  Integer  
       Dim  pageheader  As  Integer  
       Dim  pagefooter  As  Integer  
       Dim  pageright  As  Integer  
       Dim  startx  As  Integer  
       Dim  starty  As  Integer  
       Dim  startline  As  Integer  
       Dim  endline  As  Integer  
       Dim  usewidth  As  Integer  
       Dim  useheight  As  Integer  
       pageheader  =  25  
       pagefooter  =  25  
       pageleft  =  20  
       pageright  =  20  
         
       '定义纸张类型A4  
       Printer.FontSize  =  20  
       Printer.PaperSize  =  9  
       Printer.ScaleMode  =  6  
       Printer.FontBold  =  False  
       Printer.ScaleLeft  =  -20  
       Printer.ScaleTop  =  -25  
       Printer.ScaleWidth  =  210  
       Printer.ScaleHeight  =  297  
       usewidth  =  Printer.ScaleWidth  -  40  
       useheight  =  Printer.ScaleHeight  -  50  
       Printer.CurrentX  =  0  
       Printer.CurrentY  =  0  
       Printer.DrawWidth  =  2  
         
       '打印主标题  
       Printer.FontSize  =  20  
       Printer.CurrentX  =  (usewidth  -  Printer.TextWidth(Me.Text1.Text  &  "经济情况表"))  /  2  
       Printer.CurrentY  =  Printer.ScaleTop  
       Printer.Print  Me.Text1.Text  &  "经济情况表"  
         
       '打印空白行  
       Printer.FontSize  =  8  
       Printer.CurrentX  =  (usewidth  -  Printer.TextWidth(""))  /  2  
       Printer.CurrentY  =  Printer.CurrentY  +  1  
       Printer.Print  ""  
         
       '打印副标题  
       Printer.FontSize  =  15  
       Printer.CurrentX  =  (usewidth  -  Printer.TextWidth(commonth  &  "月份经济情况报表"))  /  2  
       Printer.CurrentY  =  Printer.CurrentY  +  1  
       Printer.Print  commonth  &  "月份经济情况报表"  
         
       '打印空白行  
       Printer.FontSize  =  8  
       Printer.CurrentX  =  (usewidth  -  Printer.TextWidth(""))  /  2  
       Printer.CurrentY  =  Printer.CurrentY  +  1  
       Printer.Print  ""  
         
       '打印表的内容  
       '首先打印第一条横线  
       '打印最上边的第一条横线  
       Printer.CurrentX  =  pageleft  +  Printer.ScaleLeft  
       startline  =  Printer.CurrentY  +  1  
       Printer.Line  -((Printer.ScaleLeft  +  Printer.ScaleWidth  -  pageleft),  Printer.CurrentY)  
         
         
       Printer.FontSize  =  12  
       Printer.CurrentY  =  Printer.CurrentY  +  1  
       starty  =  Printer.CurrentY  
       Printer.CurrentX  =  ((Printer.ScaleWidth  -  40)  /  3  -  Printer.TextWidth(""))  /  2  
       Printer.Print  ""  
         
       Printer.FontSize  =  12  
       Printer.CurrentX  =  usewidth  /  3  +  ((usewidth  /  3  -  Printer.TextWidth("今年经济情况"))  /  2)  
       Printer.CurrentY  =  starty  
       Printer.Print  "今年经济情况(万元)"  
         
       Printer.FontSize  =  12  
       Printer.CurrentX  =  (usewidth  /  3)  *  2  +  ((usewidth  /  3  -  Printer.TextWidth("去年经济情况"))  /  2)  
       Printer.CurrentY  =  starty  
       Printer.Print  "去年经济情况(万元)"  
         
       Printer.CurrentX  =  pageleft  +  Printer.ScaleLeft  
       Printer.CurrentY  =  Printer.CurrentY  +  1  
       Printer.Line  -((Printer.ScaleLeft  +  Printer.ScaleWidth  -  pageleft),  Printer.CurrentY)  
         
       Printer.CurrentY  =  Printer.CurrentY  +  1  
         
       For  i  =  0  To  10  
       '判断是否该页已打满  
       Me.MSFlexGrid1.Row  =  i  
       If  Printer.CurrentY  >=  useheight  Then  
 
       '打印横线  
             Printer.CurrentX  =  Printer.ScaleLeft  +  pageleft  
             Printer.Line  -((Printer.ScaleLeft  +  Printer.ScaleWidth  -  pageleft),  Printer.CurrentY)  
             Printer.CurrentY  =  Printer.CurrentY  +  1  
         
           
               '打印四条竖线  
               endline  =  Printer.CurrentY  
               Printer.Line  (0,  startline)-(0,  endline)  
               Printer.Line  (usewidth  /  3,  startline)-(usewidth  /  3,  endline)  
               Printer.Line  ((usewidth  /  3)  *  2,  startline)-((usewidth  /  3)  *  2,  endline)  
               Printer.Line  (usewidth,  startline)-(usewidth,  endline)  
                 
             '打印页号  
               Printer.CurrentX  =  (Printer.ScaleWidth  -  Printer.TextWidth(Printer.Page))  /  3  -  pageleft  
               Printer.CurrentY  =  useheight  +  3  
               Printer.Print  Printer.Page  
               Printer.NewPage    'NewPage告诉打印机,程序对当前输出页的发送已经结束。Printer对象应开始新的一页。  
               
               Printer.CurrentX  =  pageleft  +  Printer.ScaleLeft  
               Printer.CurrentY  =  pageheader  +  Printer.ScaleTop  
               startline  =  Printer.CurrentY  
               
     Else  
       '打印一行数据  
       Me.MSFlexGrid1.Col  =  0  
       Printer.CurrentX  =  ((Printer.ScaleWidth  -  40)  /  3  -  Printer.TextWidth(Me.MSFlexGrid1.Text))  /  8  
       starty  =  Printer.CurrentY  
       Printer.Print  Me.MSFlexGrid1.Text  
         
       Me.MSFlexGrid1.Col  =  1  
       Printer.CurrentX  =  (Printer.ScaleWidth  -  40)  /  3  +  ((Printer.ScaleWidth  -  40)  /  3  -  Printer.TextWidth(Me.MSFlexGrid1.Text))  /  2  
       Printer.CurrentY  =  starty  
       Printer.Print  Me.MSFlexGrid1.Text  
         
       Me.MSFlexGrid1.Col  =  2  
       Printer.CurrentX  =  ((Printer.ScaleWidth  -  40)  /  3)  *  2  +  ((Printer.ScaleWidth  -  40)  /  3  -  Printer.TextWidth(Me.MSFlexGrid1.Text))  /  2  
       Printer.CurrentY  =  starty  
       Printer.Print  Me.MSFlexGrid1.Text  
         
       Printer.CurrentX  =  pageleft  +  Printer.ScaleLeft  
       Printer.CurrentY  =  Printer.CurrentY  +  1  
       Printer.Line  -((Printer.ScaleLeft  +  Printer.ScaleWidth  -  pageleft),  Printer.CurrentY)  
         
       Printer.CurrentY  =  Printer.CurrentY  +  1  
   End  If  
Next  i  
             '打印最后一条横线  
       Printer.CurrentX  =  Printer.ScaleLeft  +  pageleft  
       Printer.Line  -((Printer.ScaleLeft  +  Printer.ScaleWidth  -  pageleft),  Printer.CurrentY)  
       endline  =  Printer.CurrentY  
       '打印四条竖线  
       Printer.Line  (0,  startline)-(0,  endline)  
       Printer.Line  (usewidth  /  3,  startline)-(usewidth  /  3,  endline)  
       Printer.Line  ((usewidth  /  3)  *  2,  startline)-((usewidth  /  3)  *  2,  endline)  
       Printer.Line  (usewidth,  startline)-(usewidth,  endline)  
       
   
               '打印页号  
             
       Printer.CurrentX  =  (Printer.ScaleWidth  -  Printer.TextWidth(Printer.Page))  /  3  -  pageleft  
       Printer.CurrentY  =  useheight  +  3  
       'Printer.Print  Printer.Page  
       Printer.Print  "        哈尔滨高新技术开发区"  
       Printer.EndDoc    'EndDoc告诉VISUAL  BASIC,程序创建文档结束。VISUAL  BASIC应将它发送到物理打印机上打印。  
 
 
 
 
   If  Err.Number  =  0  Then  
         MsgBox  "you  are  successful!",  ,  ""  
   End  If  
End  Sub
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Option Explicit Global Const mm = 567 Global Const cm = 567 Global Const NM_PP_Ofs = 0 '36 Global Const Gray = &HC0C0C0 Global Scala As Single Global Const ANTEPRIMA = 0 Global Const STAMPANTE = 1 Global Const NONESCLUSIVO = 0 Global Const ESCLUSIVO = 1 Global LocPerc As String Global Const LocName = "_$$_TEMP.TMP" Global Ofs As Single Global Const SistemaCoordinate = 0 Global NM_AnnullaStampa As Boolean Global TempDemoMode As Boolean Sub SistemaBarra(sP As Integer, eP As Integer, aP As Integer) ' PrnPRN.sBar > max bar ' PrnPRN.aBar > actual value ' ' sP = start page ' eP = end page ' aP = actual page Static Stp As Single Stp = PrnPrn.tBar.Width / ((eP - sP) + 1) PrnPrn.pBar.Width = Stp * aP End Sub Function TempFileExists(MyFilename As String) As Boolean Dim TempAttr As Double TempFileExists = True On Error GoTo MyErrorFileExist TempAttr = FileLen(MyFilename) GoTo MyExitFileExist MyErrorFileExist: TempFileExists = False Resume MyExitFileExist MyExitFileExist: On Error GoTo 0 End Function Sub ContaPagine() PrnPrv.MousePointer = vbHourglass Static NumPag As Integer NumPag = 0 Static A As String, B As String PrnPrv.ePag.Clear Open LocPerc + LocName For Append As #27: Close #27 '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Open LocPerc + LocName For Input As #27 While Not EOF(27) Line Input #27, A If A = "#startpage" Then NumPag = NumPag + 1 ElseIf A = "#endpage" Then PrnPrv.ePag.AddItem Format(NumPag) End If Wend Close #27 If PrnPrv.ePag.ListCount > 0 Then PrnPrv.ePag.ListIndex = 0 Else PrnPrv.MousePointer = vbDefault MsgBox "No pages to print!", vbInformation, "Preview non available" Unload PrnPrv End If PrnPrv.MousePointer = vbDefault End Sub Sub SistemaStatusBar()
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值