vb控制打印機自動打印表格(共15頁)_第1頁
vb控制打印機自動打印表格(共15頁)_第2頁
vb控制打印機自動打印表格(共15頁)_第3頁
vb控制打印機自動打印表格(共15頁)_第4頁
vb控制打印機自動打印表格(共15頁)_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上VB控制EXCLE自動打印表格VB是常用的應(yīng)用軟件開發(fā)工具之一,由于VB的報表功能有限,而且一但報表格式發(fā)生變化,就得相應(yīng)修改程序,給應(yīng)用軟件的維護工作帶來極大的不便。因此充分利用EXECL的強大報表功來實現(xiàn)報表功能。但由于VB與EXCEL由于分別屬于不同的應(yīng)用系統(tǒng),如何把它們有機地結(jié)合在一起,是一個值得我們研究的課題。一、 VB讀寫EXCEL表:VB本身提自動化功能可以讀寫EXCEL表,其方法如下:1、在工程中引用Microsoft Excel類型庫:從工程菜單中選擇引用欄;選擇Microsoft Excel 9.0 Object Library(EXCEL2003

2、),然后選擇確定。表示在工程中要引用EXCEL類型庫。2、在通用對象的聲明過程中定義EXCEL對象:Dim excel As ObjectDim workbook As ObjectDim sheet As Object3、在程序中操作EXCEL表常用命令:Set xlApp = CreateObject(Excel.Application) 創(chuàng)建EXCEL對象Set xlBook = xlApp.Workbooks.Open(文件名) 打開已經(jīng)存在的EXCEL工件簿文件xlApp.Visible = True 設(shè)置EXCEL對象可見(或不可見)Set xlSheet = xlBook.Wor

3、ksheets(表名) 設(shè)置活動工作表xlSheet.Cells(row, col) =值 給單元格(row,col)賦值xlSheet.PrintOut 打印工作表xlBook.Close (True) 關(guān)閉工作簿xlApp.Quit 結(jié)束EXCEL對象Set xlApp = Nothing 釋放xlApp對象xlBook.RunAutoMacros (xlAutoOpen) 運行EXCEL啟動宏xlBook.RunAutoMacros (xlAutoClose) 運行EXCEL關(guān)閉宏4、在運用以上VB命令操作EXCEL表時,除非設(shè)置EXCEL對象不可見,否則VB程序可繼續(xù)執(zhí)行其它操作,也能

4、夠關(guān)閉EXCEL,同時也可對EXCEL進行操作。但在EXCEL操作過程中關(guān)閉EXCEL對象時,VB程序無法知道,如果此時使用EXCEL對象,則VB程序會產(chǎn)生自動化錯誤。形成VB程序無法完全控制EXCEL的狀況,使得VB與EXCEL脫節(jié)。二、 EXCEL的宏功能:EXCEL提供一個Visual Basic編輯器,打開Visual Basic編輯器,其中有一工程屬性窗口,點擊右鍵菜單的插入模塊,則增加一個模塊1,在此模塊中可以運用Visual Basic語言編寫函數(shù)和過程并稱之為宏。其中,EXCEL有兩個自動宏:一個是啟動宏(Sub Auto_Open()),另一個是關(guān)閉宏(Sub Auto_Cl

5、ose())。它們的特性是:當用EXCEL打含有啟動宏的工簿時,就會自動運行啟動宏,同理,當關(guān)閉含有關(guān)閉宏的工作簿時就會自動運行關(guān)閉宏。但是通過VB的自動化功能來調(diào)用EXCEL工作表時,啟動宏和關(guān)閉宏不會自動運行,而需要在VB中通過命令xlBook.RunAutoMacros (xlAutoOpen)和xlBook.RunAutoMacros (xlAutoClose) 來運行啟動宏和關(guān)閉宏。三、 VB與EXCEL的相互勾通:充分利用EXCEL的啟動宏和關(guān)閉宏,可以實現(xiàn)VB與EXCEL的相互勾通,其方法如下:在EXCEL的啟動宏中加入一段程序,其功能是在磁盤中寫入一個標志文件,同時在關(guān)閉宏中加

6、入一段刪除此標志文件的程序。VB程序在執(zhí)行時通過判斷此標志文件存在與否來判斷EXCEL是否打開,如果此標志文件存在,表明EXCEL對象正在運行,應(yīng)該禁止其它程序的運行。如果此標志文件不存在,表明EXCEL對象已被用戶關(guān)閉,此時如果要使用EXCEL對象運行,必須重新創(chuàng)建EXCEL對象。四、VB控制EXCLE自動打印表格:1、在VB中,建立一個FORM2,界面如下:其中要求輸入的參數(shù)是原表格的縱列,即A列對應(yīng)1、B列對應(yīng)2、A列對應(yīng)1、C列對應(yīng)3、D列對應(yīng)4、E列對應(yīng)5,依此類推,打印時間是控制每打一張表所須時間,單位為毫秒(ms)。當把值設(shè)為0時,對應(yīng)單元格的內(nèi)容不變,以便靈活應(yīng)用,須把要打印的

7、表放到C盤,放到別處須要改變程序。然后在其中輸入如下程序:Option Explicit Public uint As Integer 單位名稱 Public goods As Integer 設(shè)備名稱 Public number As Integer 設(shè)備編號 Public address As Integer 出廠地址 Public modle As Integer 設(shè)備型號 Public reference As Integer 參考 Public result As Integer 檢定結(jié)果 Public dates As Integer 檢定日期 Public death As In

8、teger 檢定日期 Public cel As Integer 打印張數(shù) Public time As Integer 打印時間Private Sub Command1_Click() Form2.Hide Form1.ShowEnd SubPrivate Sub Command3_Click() uint = Val(Text1.Text) 單位名稱 goods = Val(Text2.Text) 設(shè)備名稱 modle = Val(Text3.Text) 設(shè)備型號 address = Val(Text4.Text) 出廠地址 number = Val(Text5.Text) 設(shè)備編號 re

9、ference = Val(Text6.Text) 參考 result = Val(Text7.Text) 檢定結(jié)果 dates = Val(Text8.Text) 檢定日期 time = Val(Text9.Text) 打印時間 death = Val(Text10.Text) 有效期至 MsgBox !- 參數(shù)修改成功-!End SubPrivate Sub Form_Initialize() 數(shù)據(jù)初始化 cel = 1 打印張數(shù) uint = 1 單位名稱 goods = 2 設(shè)備名稱 number = 3 設(shè)備編號 address = 4 出廠地址 modle = 5 設(shè)備型號 ref

10、erence = 6 參考 result = 7 檢定結(jié)果 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 = C

11、reateObject(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 設(shè)置表2為活動表 If Form2.uint 0 Then sheet(2).Cells(1, 2) = sheet(1).Cells(Form2.cel, Form2.uint) 單位名稱 If For

12、m2.goods 0 Then sheet(2).Cells(2, 2) = sheet(1).Cells(Form2.cel, Form2.goods) 產(chǎn)品名稱 If Form2.number 0 Then sheet(2).Cells(3, 3) = sheet(1).Cells(Form2.cel, Form2.number) 設(shè)備編號 If Form2.address 0 Then sheet(2).Cells(4, 2) = sheet(1).Cells(Form2.cel, Form2.address) 設(shè)備廠址 If Form2.modle 0 Then sheet(2).Ce

13、lls(5, 2) = sheet(1).Cells(Form2.cel, Form2.modle) 設(shè)備型號 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) 檢定結(jié)果 If Form2.dates 0 Then sheet(2).Cells(11, 2) = sheet(1).Ce

14、lls(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() 關(guān)閉退出表格 If Dir(C:excel.bz) Then workbook.Application.Run auto_close Set excel = Nothing workbook.Close (True) End I

15、f 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() 繼續(xù)打印 If Dir(C:excel.bz) = Then MsgBox !-請打開要打印的表格-! E

16、lse 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) = Th

17、en 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) 產(chǎn)品名稱 If Form2.number 0 Then sheet(2).Cells(3, 3) = sheet(1).Cells(present, Form2.n

18、umber) 設(shè)備編號 If Form2.address 0 Then sheet(2).Cells(4, 2) = sheet(1).Cells(present, Form2.address) 設(shè)備廠址 If Form2.modle 0 Then sheet(2).Cells(5, 2) = sheet(1).Cells(present, Form2.modle) 設(shè)備型號 If Form2.reference 0 Then sheet(2).Cells(6, 2) = sheet(1).Cells(present, Form2.reference) 分度號 If Form2.result

19、0 Then sheet(2).Cells(7, 2) = sheet(1).Cells(present, Form2.result) 檢定結(jié)果 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 Di

20、r(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) 產(chǎn)品名稱 If Form2.number 0 Then shee

21、t(2).Cells(3, 3) = sheet(1).Cells(present, Form2.number) 設(shè)備編號 If Form2.address 0 Then sheet(2).Cells(4, 2) = sheet(1).Cells(present, Form2.address) 設(shè)備廠址 If Form2.modle 0 Then sheet(2).Cells(5, 2) = sheet(1).Cells(present, Form2.modle) 設(shè)備型號 If Form2.reference 0 Then sheet(2).Cells(6, 2) = sheet(1).Ce

22、lls(present, Form2.reference) 分度號 If Form2.result 0 Then sheet(2).Cells(7, 2) = sheet(1).Cells(present, Form2.result) 檢定結(jié)果 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) 有效期至 E

23、nd 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 = F

24、alse a = sheet(1).Cells(Form2.cel, 2) 如果單位名稱為“”則打印結(jié)束 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) 產(chǎn)品名稱 If Form2.number 0 Then sheet(2).Cells(3, 3) = sheet(1).Cells(Form2.cel, Form2.number) 設(shè)備編號 If Form2.address 0 Then sheet(2).Cells(4, 2) = sheet(1).Cells(Form2.cel, Form2.address) 設(shè)備廠址 If Form2.modle 0 Then sheet(2).Cells(5, 2) = sheet(1).Ce

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論