本文程序资源下载地址:点击打开链接
工作中经常遇到打印的问题,但很多时候复杂的打印需要编程来实现,下面就介绍VB中两种简单打印的方法
利用数组进行打印请参看博文:点击打开
(一) 打印窗体
直接像截图一样把窗体打出来,命令如下
Me.PrintForm
(二) 打印出一个Excel表格
Private Sub Command2_Click() Set exl = New Excel.Application exl.Visible = True exl.SheetsInNewWorkbook = 1 Set wbook = exl.Workbooks.Add With exl.ActiveSheet.Range("A2:C9").Borders '边框设置 .LineStyle = 1 'xlBorderLineStyleContinuous .Weight = xlThin .ColorIndex = 1 End With With exl.ActiveSheet.Range("A3:C9").Font '字体设置 .Size = 14 .Bold = True .Italic = True .ColorIndex = 3 End With exl.ActiveSheet.Rows.HorizontalAlignment = xlVAlignCenter '水平居中 exl.ActiveSheet.Rows.VerticalAlignment = xlVAlignCenter '垂直居中 With exl.ActiveSheet .Cells(1,2).Value = "100" .Cells(2,2).Value = "200" .Cells(3,2).Value = "=SUM(B1:B2)" .Cells(1,3).Value = "打印表格" .Range("A3:A9") = "50" End With exl.ActiveSheet.PageSetup.Orientation = xlPortrait 'xlLandscape exl.ActiveSheet.PageSetup.PaperSize = xlPaperA4 exl.ActiveSheet.PrintOut exl.DisplayAlerts = False exl.Quit exl.DisplayAlerts = True Set exl = Nothing End Sub
打印后的结果:
通过代码,我们可以读出
A2:C9进行了边框设置;
A3:A9字体加粗、斜体;数值为50;B3为B1和B2之和