vb控制打印機(jī)自動(dòng)打印表格_第1頁
vb控制打印機(jī)自動(dòng)打印表格_第2頁
vb控制打印機(jī)自動(dòng)打印表格_第3頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、vb控制打印機(jī)自動(dòng)打印表格VB控制EXCLE自動(dòng)打印表格VB是常用的應(yīng)用軟件開發(fā)工具之一,由于VB的報(bào)表功能有限, 而且一但報(bào)表格式發(fā)生變化,就得相應(yīng)修改程序,給應(yīng)用軟件的維護(hù) 工作帶來極大的不便。因此充分利用EXECL的強(qiáng)大報(bào)表功來實(shí)現(xiàn)報(bào) 表功能。但由于VB與EXCEL由于分別屬于不同的應(yīng)用系統(tǒng),如何 把它們有機(jī)地結(jié)合在一起,是一個(gè)值得我們研究的課題。一、VB 讀寫 EXCEL 表:VB本身提自動(dòng)化功能可以讀寫EXCEL表,其方法如下:*1、在工程中引用Microsoft Excel類型庫:從“工程”菜單中選擇”引用'欄;選擇Microsoft Excel 9.0 Object Li

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

3、= xlBook.Worksheets(”表名”)'設(shè)置活動(dòng)工作表 xlSheet.Cells(row, col)=值,給單元格(row,col)賦值 xISheet.PrintOut '打印工作表xlBook.Close仃rue),關(guān)閉工作簿xIApp.Quit 結(jié)束 EXCEL 對象Set xIApp = Nothing,釋放 xIApp 對象 xlBook.RunAutoMacros (xIAutoOpen),運(yùn)行 EXCEL 啟動(dòng)宏 xlBook.RunAutoMacros (xIAutoClose) 運(yùn)行 EXCEL 關(guān)閉宏4、在運(yùn)用以上VB命令操作EXCEL表時(shí),除

4、非設(shè)置EXCEL對象不可見,否則VB程序可繼續(xù)執(zhí)行其它操作,也能夠關(guān)閉 EXCEL,同時(shí)也可對EXCEL進(jìn)行操作。但在EXCEL操作過程中 關(guān)閉EXCEL對象時(shí),VB程序無法知道,如果此時(shí)使用EXCEL 對象,則VB程序會(huì)產(chǎn)生自動(dòng)化錯(cuò)誤。形成VB程序無法完全控制 EXCEL的狀況,使得VB與EXCEL脫節(jié)。二、EXCEL的宏功能:EXCEL 提供一個(gè) Visual Basic 編輯器,打開 Visual Basic 編輯器,其中有一工程屬性窗口,點(diǎn)擊右鍵菜單的”插入模塊”,則 增加一個(gè)”模塊1M,在此模塊中可以運(yùn)用Visual Basic語言編寫 函數(shù)和過程并稱之為宏。其中,EXCEL有兩個(gè)自

5、動(dòng)宏:一個(gè)是啟 動(dòng)宏(Sub Auto_Open(),另一個(gè)是關(guān)閉宏(Sub Auto_Close()o 它們的特性是:當(dāng)用EXCEL打含有啟動(dòng)宏的工簿時(shí),就會(huì)自動(dòng)運(yùn) 行啟動(dòng)宏,同理,當(dāng)關(guān)閉含有關(guān)閉宏的工作簿時(shí)就會(huì)自動(dòng)運(yùn)行關(guān)閉宏。但是通過VB的自動(dòng)化功能來調(diào)用EXCEL I作表時(shí),啟動(dòng)宏 和關(guān)閉宏不會(huì)自動(dòng)運(yùn)行,而需要在VB中通過命令 xlBook.R unAu toMacros(xIAutoOpen)和xlBook.RunAutoMacros (xIAutoClose)來運(yùn)行啟動(dòng)宏和關(guān)閉 宏。三、VB與EXCEL的相互勾通:充分利用EXCEL的啟動(dòng)宏和關(guān)閉宏,可以實(shí)現(xiàn)VB與EXCEL 的相互勾

6、通,其方法如下:在EXCEL的啟動(dòng)宏中加入一段程序,其功能是在磁盤中寫入 一個(gè)標(biāo)志文件,同時(shí)在關(guān)閉宏中加入一段刪除此標(biāo)志文件的程序。 VB程序在執(zhí)行時(shí)通過判斷此標(biāo)志文件存在與否來判斷EXCEL是 否打開,如果此標(biāo)志文件存在,表明EXCEL對象正在運(yùn)行,應(yīng)該 禁止其它程序的運(yùn)行。如果此標(biāo)志文件不存在,表明EXCEL對象 已被用戶關(guān)閉,此時(shí)如果要使用EXCEL對象運(yùn)行,必須重新創(chuàng)建 EXCEL對象。四、VB控制EXCLE自動(dòng)打印表格:1、在VB中,建立一個(gè)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,依此類推,

7、打印時(shí)間是控 制每打一張表所須時(shí)間,單位為毫秒(ms) o當(dāng)把值設(shè)為0時(shí),對 應(yīng)單元格的內(nèi)容不變,以便靈活應(yīng)用,須把要打印的表放到C盤, 放到別處須要改變程序。O然后在其中輸入如下程序:Option ExplicitPublic uint As Integer'單位名稱Public goods As Integer設(shè)備名稱Public number As Integer'設(shè)備編號(hào)Public address As Integer'出廠地址Public modle As Integer'設(shè)備型號(hào)Public reference As Integer滲考Public

8、 result As Integer檢定結(jié)果Public dates As Integer'檢定日期Public death As Integer檢定日期Public cel As Integer打印張數(shù)Public time As Integer,打印時(shí)間Private Sub Command1_Click()Form2.HideForml.ShowEnd SubPrivate Sub Command3_Click()uint = Val(Text1 .Text)'單位名稱goods = Val(Text2.Text)設(shè)備名稱modle = Val(Text3.Text)設(shè)備

9、型號(hào)address = Val(Text4.Text)'出廠地址number = Val(Text5.Text)'設(shè)備編號(hào)reference = Val(Text6.Text)參考result = Val(Text7.Text)檢定結(jié)果dates = Val(Text 8.Text)'檢定日期time = Val(T ext9.Text)'打印時(shí)間death = Val(TextlO.Text)有效期至MsgBox M! A-A參數(shù)修改成功A-A! MEnd Sub'數(shù)據(jù)初始化Private Sub Form_lnitialize()cel = 1

10、9;打印張數(shù)'單位名稱goods=2'設(shè)備名稱number = 3'設(shè)備編號(hào)address = 4'出廠地址modle=5'設(shè)備型號(hào)reference = 6參考result:=7'檢定結(jié)果dates :=8'檢定日期death :=9有效期至time =2000'打印時(shí)間間隔End SubPrivate Sub Command2_Click()EndEnd Sub建立一個(gè)FORM1,界面如下:然后在其中輸入如下程序:Dim excel As ObjectDim workbook As ObjectDim sheet As Obj

11、ectDim present%Private Sub Command1_Click()'打開 EXCLE 表格If Dir(MC:excel.bzM) = ThenSet excel = CreateObject(Mexcel.applicationvv)flc: 自動(dòng)打印表格.Xis-)Set sheet = workbook.WorkSheetsexcel.Visible = TrueMauto_operT excel.WorkSheets(2).Activate,設(shè)置表 2 為活動(dòng)表If Form2.uint <> 0 Then sheetCells(1, 2)=sh

12、eet(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)='設(shè)備編號(hào)sheet(1 ).Cells(Form2.cel, Form2.number)If Form2.address <> 0 Then sheet(2).Cells(4, 2)= sheet(

13、1 ).Cells(Form2.cel, Form2.address) * 設(shè)備廠址If Form2.modle <> 0 Then sheet(2).Cells(5, 2)=sheet(1 ).Cells(Form2.cel, Form2.modle),設(shè)備型號(hào)If Form2.reference <> 0 Then sheet(2).Cells(6, 2)= sheet(1 ).Cells(Form2.cel, Form2.reference) 分度號(hào)If Form2.result <> 0 Then sheet(2).Cells(7, 2)=sheet

14、(1 ).Cells(Form2.cel, Form2.result) ,檢定結(jié)果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),有效期至ElseMsgBox MEXCL 已打開!”End IfEnd SubPrivate Sub Command2_Click(),關(guān)

15、閉退岀表格If Dir(HC:excel.bzMM Set excel = Nothing workbook.Close (True)End IfForml.HideForm2.ShowEnd SubPrivate Sub Command3_Click(),暫停打印If Dir(MC:excel.bzM) = ThenMsgBox M!心八請打開要打印的表格A-A! MElseTimerl.Enabled = FalseMsgBox 11!心八打印暫停A-A! ,f &Chr(10) &”!兒八已打印”& Form2.cel present -1 & ”張心八!

16、 ”End IfEnd SubPrivate Sub Command4_Click(),繼續(xù)打印If Dir(MC:excel.bzM) = ” ThenMsgBox M!心八請打開要打印的表格心八!”ElseTimerln terval = Form2.timeTimerl.Enabled = TrueEnd IfEnd Sub'開始打印Private Sub Command5_Click()If Dir(HC:excel.bzM) = ThenMsgBox M!心八請打開要打印的表格A_A|Elsepresent = 0Form2.cel = 1Timerln terval = F

17、orm2.timeTimerl.Enabled = TrueEnd IfEnd SubPrivate Sub Command6_Click()下一張If Dir(HC:excel.bzM) = ThenMsgBox n!心八請打開要打印的表格A-A! MElsepresent = prese nt + 1If Form2.uint <> 0 Then sheetCells(1, 2)=sheet(1 ).Cells(present, Form2.uint),單位名稱If Form2.goods <> 0 Then sheetCells(2, 2)=sheet(1 ).C

18、ells(present, Form2.goods),產(chǎn)品名稱If Form2.number <> 0 Then sheet(2).Cells(3, 3)=sheet(1 ).Cells(present, Form2.number),設(shè)備編號(hào)If Form2.address <> 0 Then sheetCells(4, 2)= sheet(1 ).Cells(present, Form2.address)1 設(shè)備廠址If Form2.modle <> 0 Then sheet(2).Cells(5, 2)= sheet(1 ).Cells(present,

19、 Form2.modle)設(shè)備型號(hào)If Form2.reference <> 0 Then sheet(2).Cells(6, 2)= sheet(1 ).Cells(present, Form2.reference),分度號(hào)IfForm2.result<>0Thensheet(2).Cells(7,2)=sheet(1 ).Cells(present, Form2.result)1 檢定結(jié)果IfForm2.dates<>0ThensheetCells(11,2)=sheet(1 ).Cells(present, Form2.dates),檢定日期IfFor

20、m2.death<>0ThensheetCells(12,3)=sheet(1 ).Cells(present, Form2.death),有效期至End IfEnd Sub上一Private Sub Command7_Click()張If Dir(HC:excel.bzM) = " ThenMsgBox M!心八請打開要打印的表格A-A! MElsepresent = present -1If present <= 0 Then present = 1If Form2.uint <> 0 Then sheetCells(1, 2)=sheet(1 ).

21、Cells(present, Form2.uint),單位名稱If Form2.goods <> 0 Then sheetCells(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.number),設(shè)備編號(hào)If Form2.address <> 0 Then sheetCells(4, 2)=sheet(1 ).Cells(present, Form2.ad

22、dress)* 設(shè)備廠址If Form2.modle <> 0 Then sheet(2).Cells(5, 2)= sheet(1 ).Cells(present, Form2.modle)設(shè)備型號(hào)If Form2.reference <> 0 Then sheet(2).Cells(6, 2)=sheet(1 ).Cells(present, Form2.reference),分度號(hào)If Form2.result <> 0 Then sheet(2).Cells(7, 2)=sheet(1 ).Cells(present, Form2.result),檢

23、定結(jié)果If Form2.dates <> 0 Then sheetCells(11, 2)=sheet(1 ).Cells(present, Form2.dates),檢定日期If Form2.death <> 0 Then sheetCells(12, 3)=sheet(1 ).Cells(present, Form2.death),有效期至End IfEnd SubPrivate Sub Command8_Click(),從當(dāng)錢頁打印If Dir(nC:exceLbzfl) = Hff ThenMsgBox M!心八請打開要打印的表格A-A! MElseForm2.

24、cel = presentTimerl.E nabled = TrueEnd IfEnd SubPrivate Sub Form_Load()prese nt = 0Timerln terval = Form2.timeTimerl.Enabled = FalseEnd SubPrivate Sub Timer1_Timer()Dim a$Timerl.E nabled = Falsea = sheet(1).Cells(Form2.cel, 2),如果單位名稱為"則打印結(jié)束If a <> ThenIf Form2.uint <> 0 Then sheetCe

25、lls(1, 2)=sheet(1 ).Cells(Form2.cel, Form2.uint),單位名稱If Form2.goods <> 0 Then sheetCells(2, 2)=產(chǎn)品名稱sheet(1 ).Cells(Form2.cel, Form2.goods)If Form2.number <> 0 Then sheet(2).Cells(3, 3)= sheet(1 ).Cells(Form2.cel, Form2.number),設(shè)備編號(hào)If Form2.address <> 0 Then sheetCells(4, 2)= sheet(1 ).Cells(Form2.cel, Form2.address) * 設(shè)備廠址If Form2.modle <> 0 Then sheetCells(5, 2)= sheet(1 ).Cell

溫馨提示

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

評論

0/150

提交評論