vb控制打印机自动打印表格.doc

上传人:wux****ua 文档编号:8973476 上传时间:2020-04-02 格式:DOC 页数:15 大小:219.50KB
返回 下载 相关 举报
vb控制打印机自动打印表格.doc_第1页
第1页 / 共15页
vb控制打印机自动打印表格.doc_第2页
第2页 / 共15页
vb控制打印机自动打印表格.doc_第3页
第3页 / 共15页
点击查看更多>>
资源描述
VB控制EXCLE自动打印表格VB是常用的应用软件开发工具之一,由于VB的报表功能有限,而且一但报表格式发生变化,就得相应修改程序,给应用软件的维护工作带来极大的不便。因此充分利用EXECL的强大报表功来实现报表功能。但由于VB与EXCEL由于分别属于不同的应用系统,如何把它们有机地结合在一起,是一个值得我们研究的课题。一、 VB读写EXCEL表:VB本身提自动化功能可以读写EXCEL表,其方法如下:1、在工程中引用Microsoft Excel类型库:从工程菜单中选择引用栏;选择Microsoft Excel 9.0 Object Library(EXCEL2003),然后选择确定。表示在工程中要引用EXCEL类型库。2、在通用对象的声明过程中定义EXCEL对象:Dim excel As ObjectDim workbook As ObjectDim sheet As Object3、在程序中操作EXCEL表常用命令:Set xlApp = CreateObject(Excel.Application) 创建EXCEL对象Set xlBook = xlApp.Workbooks.Open(文件名) 打开已经存在的EXCEL工件簿文件xlApp.Visible = True 设置EXCEL对象可见(或不可见)Set xlSheet = xlBook.Worksheets(表名) 设置活动工作表xlSheet.Cells(row, col) =值 给单元格(row,col)赋值xlSheet.PrintOut 打印工作表xlBook.Close (True) 关闭工作簿xlApp.Quit 结束EXCEL对象Set xlApp = Nothing 释放xlApp对象xlBook.RunAutoMacros (xlAutoOpen) 运行EXCEL启动宏xlBook.RunAutoMacros (xlAutoClose) 运行EXCEL关闭宏4、在运用以上VB命令操作EXCEL表时,除非设置EXCEL对象不可见,否则VB程序可继续执行其它操作,也能够关闭EXCEL,同时也可对EXCEL进行操作。但在EXCEL操作过程中关闭EXCEL对象时,VB程序无法知道,如果此时使用EXCEL对象,则VB程序会产生自动化错误。形成VB程序无法完全控制EXCEL的状况,使得VB与EXCEL脱节。二、 EXCEL的宏功能:EXCEL提供一个Visual Basic编辑器,打开Visual Basic编辑器,其中有一工程属性窗口,点击右键菜单的插入模块,则增加一个模块1,在此模块中可以运用Visual Basic语言编写函数和过程并称之为宏。其中,EXCEL有两个自动宏:一个是启动宏(Sub Auto_Open()),另一个是关闭宏(Sub Auto_Close())。它们的特性是:当用EXCEL打含有启动宏的工簿时,就会自动运行启动宏,同理,当关闭含有关闭宏的工作簿时就会自动运行关闭宏。但是通过VB的自动化功能来调用EXCEL工作表时,启动宏和关闭宏不会自动运行,而需要在VB中通过命令xlBook.RunAutoMacros (xlAutoOpen)和xlBook.RunAutoMacros (xlAutoClose) 来运行启动宏和关闭宏。三、 VB与EXCEL的相互勾通:充分利用EXCEL的启动宏和关闭宏,可以实现VB与EXCEL的相互勾通,其方法如下:在EXCEL的启动宏中加入一段程序,其功能是在磁盘中写入一个标志文件,同时在关闭宏中加入一段删除此标志文件的程序。VB程序在执行时通过判断此标志文件存在与否来判断EXCEL是否打开,如果此标志文件存在,表明EXCEL对象正在运行,应该禁止其它程序的运行。如果此标志文件不存在,表明EXCEL对象已被用户关闭,此时如果要使用EXCEL对象运行,必须重新创建EXCEL对象。四、VB控制EXCLE自动打印表格:1、在VB中,建立一个FORM2,界面如下:其中要求输入的参数是原表格的纵列,即A列对应1、B列对应2、A列对应1、C列对应3、D列对应4、E列对应5,依此类推,打印时间是控制每打一张表所须时间,单位为毫秒(ms)。当把值设为0时,对应单元格的内容不变,以便灵活应用,须把要打印的表放到C盘,放到别处须要改变程序。然后在其中输入如下程序:Option Explicit Public uint As Integer 单位名称 Public goods As Integer 设备名称 Public number As Integer 设备编号 Public address As Integer 出厂地址 Public modle As Integer 设备型号 Public reference As Integer 参考 Public result As Integer 检定结果 Public dates As Integer 检定日期 Public death As Integer 检定日期 Public cel As Integer 打印张数 Public time As Integer 打印时间Private Sub Command1_Click() Form2.Hide Form1.ShowEnd SubPrivate Sub Command3_Click() uint = Val(Text1.Text) 单位名称 goods = Val(Text2.Text) 设备名称 modle = Val(Text3.Text) 设备型号 address = Val(Text4.Text) 出厂地址 number = Val(Text5.Text) 设备编号 reference = Val(Text6.Text) 参考 result = Val(Text7.Text) 检定结果 dates = Val(Text8.Text) 检定日期 time = Val(Text9.Text) 打印时间 death = Val(Text10.Text) 有效期至 MsgBox !- 参数修改成功-!End SubPrivate Sub Form_Initialize() 数据初始化 cel = 1 打印张数 uint = 1 单位名称 goods = 2 设备名称 number = 3 设备编号 address = 4 出厂地址 modle = 5 设备型号 reference = 6 参考 result = 7 检定结果 dates = 8 检定日期 death = 9 有效期至 time = 2000 打印时间间隔End SubPrivate Sub Command2_Click()EndEnd Sub建立一个FORM1,界面如下:然后在其中输入如下程序:Dim excel As ObjectDim workbook As ObjectDim sheet As ObjectDim present%Private Sub Command1_Click() 打开EXCLE表格 If Dir(C:excel.bz) = Then Set excel = CreateObject(excel.application) Set workbook = excel.Workbooks.Open(c:自动打印表格.xls) Set sheet = workbook.WorkSheets excel.Visible = True workbook.Application.Run auto_open excel.WorkSheets(2).Activate 设置表2为活动表 If Form2.uint 0 Then sheet(2).Cells(1, 2) = sheet(1).Cells(Form2.cel, Form2.uint) 单位名称 If Form2.goods 0 Then sheet(2).Cells(2, 2) = sheet(1).Cells(Form2.cel, Form2.goods) 产品名称 If Form2.number 0 Then sheet(2).Cells(3, 3) = sheet(1).Cells(Form2.cel, Form2.number) 设备编号 If Form2.address 0 Then sheet(2).Cells(4, 2) = sheet(1).Cells(Form2.cel, Form2.address) 设备厂址 If Form2.modle 0 Then sheet(2).Cells(5, 2) = sheet(1).Cells(Form2.cel, Form2.modle) 设备型号 If Form2.reference 0 Then sheet(2).Cells(6, 2) = sheet(1).Cells(Form2.cel, Form2.reference) 分度号 If Form2.result 0 Then sheet(2).Cells(7, 2) = sheet(1).Cells(Form2.cel, Form2.result) 检定结果 If Form2.dates 0 Then sheet(2).Cells(11, 2) = sheet(1).Cells(Form2.cel, Form2.dates) 检定日期 If Form2.death 0 Then sheet(2).Cells(12, 3) = sheet(1).Cells(Form2.cel, Form2.death) 有效期至 Else MsgBox EXCL已打开! End IfEnd SubPrivate Sub Command2_Click() 关闭退出表格 If Dir(C:excel.bz) Then workbook.Application.Run auto_close Set excel = Nothing workbook.Close (True) End If Form1.Hide Form2.ShowEnd SubPrivate Sub Command3_Click() 暂停打印 If Dir(C:excel.bz) = Then MsgBox !-请打开要打印的表格-! Else Timer1.Enabled = False MsgBox !-打印暂停-! & Chr(10) & !-已打印 & Form2.cel - present - 1 & 张-! End IfEnd SubPrivate Sub Command4_Click() 继续打印 If Dir(C:excel.bz) = Then MsgBox !-请打开要打印的表格-! Else Timer1.Interval = Form2.time Timer1.Enabled = True End IfEnd SubPrivate Sub Command5_Click() 开始打印 If Dir(C:excel.bz) = Then MsgBox !-请打开要打印的表格-! Else present = 0 Form2.cel = 1 Timer1.Interval = Form2.time Timer1.Enabled = True End IfEnd SubPrivate Sub Command6_Click() 下一张 If Dir(C:excel.bz) = Then MsgBox !-请打开要打印的表格-! Else present = present + 1 If Form2.uint 0 Then sheet(2).Cells(1, 2) = sheet(1).Cells(present, Form2.uint) 单位名称 If Form2.goods 0 Then sheet(2).Cells(2, 2) = sheet(1).Cells(present, Form2.goods) 产品名称 If Form2.number 0 Then sheet(2).Cells(3, 3) = sheet(1).Cells(present, Form2.number) 设备编号 If Form2.address 0 Then sheet(2).Cells(4, 2) = sheet(1).Cells(present, Form2.address) 设备厂址 If Form2.modle 0 Then sheet(2).Cells(5, 2) = sheet(1).Cells(present, Form2.modle) 设备型号 If Form2.reference 0 Then sheet(2).Cells(6, 2) = sheet(1).Cells(present, Form2.reference) 分度号 If Form2.result 0 Then sheet(2).Cells(7, 2) = sheet(1).Cells(present, Form2.result) 检定结果 If Form2.dates 0 Then sheet(2).Cells(11, 2) = sheet(1).Cells(present, Form2.dates) 检定日期 If Form2.death 0 Then sheet(2).Cells(12, 3) = sheet(1).Cells(present, Form2.death) 有效期至 End IfEnd SubPrivate Sub Command7_Click() 上一张 If Dir(C:excel.bz) = Then MsgBox !-请打开要打印的表格-! Else present = present - 1 If present = 0 Then present = 1 If Form2.uint 0 Then sheet(2).Cells(1, 2) = sheet(1).Cells(present, Form2.uint) 单位名称 If Form2.goods 0 Then sheet(2).Cells(2, 2) = sheet(1).Cells(present, Form2.goods) 产品名称 If Form2.number 0 Then sheet(2).Cells(3, 3) = sheet(1).Cells(present, Form2.number) 设备编号 If Form2.address 0 Then sheet(2).Cells(4, 2) = sheet(1).Cells(present, Form2.address) 设备厂址 If Form2.modle 0 Then sheet(2).Cells(5, 2) = sheet(1).Cells(present, Form2.modle) 设备型号 If Form2.reference 0 Then sheet(2).Cells(6, 2) = sheet(1).Cells(present, Form2.reference) 分度号 If Form2.result 0 Then sheet(2).Cells(7, 2) = sheet(1).Cells(present, Form2.result) 检定结果 If Form2.dates 0 Then sheet(2).Cells(11, 2) = sheet(1).Cells(present, Form2.dates) 检定日期 If Form2.death 0 Then sheet(2).Cells(12, 3) = sheet(1).Cells(present, Form2.death) 有效期至 End IfEnd SubPrivate Sub Command8_Click() 从当钱页打印 If Dir(C:excel.bz) = Then MsgBox !-请打开要打印的表格-! Else Form2.cel = present Timer1.Enabled = True End IfEnd SubPrivate Sub Form_Load() present = 0 Timer1.Interval = Form2.time Timer1.Enabled = FalseEnd SubPrivate Sub Timer1_Timer()Dim a$ Timer1.Enabled = False a = sheet(1).Cells(Form2.cel, 2) 如果单位名称为“”则打印结束 If a Then If Form2.uint 0 Then sheet(2).Cells(1, 2) = sheet(1).Cells(Form2.cel, Form2.uint) 单位名称 If Form2.goods 0 Then sheet(2).Cells(2, 2) = sheet(1).Cells(Form2.cel, Form2.goods) 产品名称 If Form2.number 0 Then sheet(2).Cells(3, 3) = sheet(1).Cells(Form2.cel, Form2.number) 设备编号 If Form2.address 0 Then sheet(2).Cells(4, 2) = sheet(1).Cells(Form2.cel, Form2.address) 设备厂址 If Form2.modle 0 Then sheet(2).Cells(5, 2) = sheet(1).Cells(Form2.cel, Form2.modle) 设备型号 If Form2.reference 0 Then sheet(2).Cells(6, 2) = sheet(1).Cells(Form2.cel, Form2.reference) 分度号 If Form2.result 0 Then sheet(2).Cells(7, 2) = sheet(1).Cells(Form2.cel, Form2.result) 检定结果 If Form2.dates 0 Then sheet(2).Cells(11, 2) = sheet(1).Cells(Form2.cel, Form2.dates) 检定日期 If Form2.death 0 Then sheet(2).Cells(12, 3) = sheet(1).Cells(Form2.cel, Form2.death) 有效期至 excel.ActiveSheet.PrintOut 打印输出 Form2.cel = Form2.cel + 1 Timer1.Interval = Form2.time Timer1.Enabled = True Else MsgBox !-表格已打完-! & Chr(10) & !-共打印 & Form2.cel - present - 1 & 张-! End IfEnd Sub4、运行VB程序,输入参数点击确定按钮可完成参数的修改,打印时间是控制每打一张表所须时间,打开EXCEL系统后,VB程序和EXCEL分别属两个不同的应用系统,均可同时进行操作,由于系统加了判断,因此在VB程序中重复点击EXCEL按钮时会提示EXCEL已打开。如果在EXCEL中关闭EXCEL后再点EXCEL按钮,则会重新打开EXCEL。而无论EXCEL打开与否,通过VB程序均可关闭EXCEL。这样就实现了VB与EXCEL的无缝连接。
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 大学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!