讀文件代碼參考: '強(qiáng)制打開(kāi)變量聲明開(kāi)關(guān) Option Explicit Const ForReading = 1,ForWriting = 2,ForAppending = 8 Dim fso,fil '創(chuàng)建fso對(duì)象 Set fso=CreateObject('Scripting.FileSystemObject') '打開(kāi)文件 Set fil=fso.OpenTextFile ('d:\calc.txt',ForReading) '讀取文件內(nèi)容 Do While not fil.AtEndOfStream MsgBox fil.ReadLine Loop '關(guān)閉文件 fil.Close '釋放文件 Set fil=Nothing Set fso=Nothing
讀取一個(gè)excel文件中每一個(gè)單元格的內(nèi)容 Option Explicit Dim xlApp,xlWorkBook,xlWorkSheet '創(chuàng)建Excel應(yīng)用程序?qū)ο?/span> Set xlApp=CreateObject('excel.application') xlApp.Visible=True '打開(kāi)指定的工作簿 Set xlWorkBook=xlApp.Workbooks.Open('d:\calc.xls') '指定工作表 Set xlWorkSheet=xlWorkBook.Sheets('Sheet1') '獲取有效區(qū)域的行數(shù)和列數(shù) Dim iRowCount,iColumnCount,i,j iRowCount=xlWorkSheet.UsedRange.Rows.Count iColumnCount=xlWorkSheet.UsedRange.Columns.Count For i=1 To iRowCount For j=1 To iColumnCount MsgBox xlWorkSheet.Cells(i,j)&vbNewLine Next Next '關(guān)閉工作簿 xlWorkBook.Close '退出excel程序 xlApp.Quit '釋放excel資源 Set xlWorkSheet=Nothing Set xlWorkBook=Nothing Set xlApp=Nothing。
Word讀寫實(shí)例 1: '打開(kāi)word應(yīng)用程序 Set WordApp = createobject ('word.application') oWordApp.visible=true msgbox 'word應(yīng)用程序被打開(kāi)' oWordApp.quit
2: '創(chuàng)建一個(gè)新文檔 Set WordApp = createobject ('word.application') Set WordDoc =oWordApp.Documents.Add '文件另存為a.doc oWordDoc.SaveAS 'c:\a.doc' msgbox '文件創(chuàng)建成功' oWordDoc.close oWordApp.quit Set WordDoc =nothing Set WordApp =nothing
3.1 ''一。打開(kāi)一個(gè)文檔 Set WordDoc = getobject ('c:\a.txt') Set WordApp = oWordDoc.Application oWordApp.visible =true msgbox '文檔已經(jīng)打開(kāi)!'
3.2 '二。打開(kāi)一個(gè)文檔 '打開(kāi)word應(yīng)用程序 Set WordApp = createobject ('word.application') oWordApp.visible=true msgbox 'word應(yīng)用程序被打開(kāi)' Set oWordDoc =oWordApp.Documents.open('c:\a.doc') msgbox '文檔已經(jīng)打開(kāi)' oWordDoc.close oWordApp.quit Set WordDoc= nothing Set WordApp =nothing
4: '在word中寫入內(nèi)容 editword 'c:\a.doc','QTP與word的操作' Function editword (filepath,content) Set WordApp = createobject ('Word.application') wordapp.Visible = true Set doc=WordApp.Documents.Open(filepath) doc.Content =content doc.Save Set doc=nothing Set WordApp = nothing ReadWord =true End Function
5:‘向一個(gè)文檔中插入表格
Function EditWord(filepath)
Set WordApp = CreateObject('Word.application') oWordApp.visible = True Set WordDoc = oWordApp.Documents.Open(filepath) oWordDoc.Range.Select Set WordSet = oWordApp.Selection With oWordSet Set NewTable = .Tables.Add(.range, 5, 3) oNewTable.Range.Font.size = 8 i = 1 oNewTable.Cell(i, 1).Range.Text = 'i' oNewTable.Cell(i, 2).Range.Text = 'i*2' oNewTable.Cell(i, 3).Range.Text = 'i*3'
For i =2 to 5 oNewTable.Cell(i, 1).Range.Text = i-1 oNewTable.Cell(i, 2).Range.Text = (i-1)*2 oNewTable.Cell(i, 3).Range.Text = (i-1)*3 Next
End with oWordDoc.save oWordApp.quit Set WordDoc = nothing Set WordApp = nothing
end function 6:’向word中插入圖片 EditWord 'd:\testbbk.doc','d:\test.bmp' Function EditWord(filepath, filepic) Set WordApp = CreateObject('Word.application') oWordApp.visible = True Set WordDoc = oWordApp.Documents.Open(filepic) oWordDoc.Range.Select Set WordSet = oWordApp.Selection With oWordSet Set Img = .InlineShapes.AddPicture(filepic, False, True) oImg.Width = oImg.Width*0.50 oImg.Height = oImg.Height*0.50 '中間對(duì)齊 oImg.Range.ParagraphFormat.Alignment = 1 .TypeParagraph .TypeText 'qtp學(xué)習(xí)之對(duì)word的基本操作' .TypeParagraph End with end function 7.‘向word中插入文本 EditWord 'C:\testbbo.doc', 'QTP學(xué)習(xí)之word' Function EditWord(filepath, content) Set WordApp = CreateObject('Word.Application') oWordApp.Visible = True Set doc = oWordApp.Documents.Open(filepath) doc.Content = content doc.save Set doc = Nothing Set WordApp = Nothing ReadWord = True
End Function
|
|