读文件代码参考:
'强制打开变量声明开关
Option Explicit
Const ForReading = 1,ForWriting = 2,ForAppending = 8
Dim fso,fil
'创建fso对象
Set fso=CreateObject('Scripting.FileSystemObject')
'打开文件
Set fil=fso.OpenTextFile ('d:\calc.txt',ForReading)
'读取文件内容
Do While not fil.AtEndOfStream
MsgBox fil.ReadLine
Loop
'关闭文件
fil.Close
'释放文件
Set fil=Nothing
Set fso=Nothing
读取一个excel文件中每一个单元格的内容
Option Explicit
Dim xlApp,xlWorkBook,xlWorkSheet
'创建Excel应用程序对象
Set xlApp=CreateObject('excel.application')
xlApp.Visible=True
'打开指定的工作簿
Set xlWorkBook=xlApp.Workbooks.Open('d:\calc.xls')
'指定工作表
Set xlWorkSheet=xlWorkBook.Sheets('Sheet1')
'获取有效区域的行数和列数
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
'关闭工作簿
xlWorkBook.Close
'退出excel程序
xlApp.Quit
'释放excel资源
Set xlWorkSheet=Nothing
Set xlWorkBook=Nothing
Set xlApp=Nothing。
Word读写实例
1:
'打开word应用程序
Set WordApp = createobject ('word.application')
oWordApp.visible=true
msgbox 'word应用程序被打开'
oWordApp.quit
2:
'创建一个新文档
Set WordApp = createobject ('word.application')
Set WordDoc =oWordApp.Documents.Add
'文件另存为a.doc
oWordDoc.SaveAS 'c:\a.doc'
msgbox '文件创建成功'
oWordDoc.close
oWordApp.quit
Set WordDoc =nothing
Set WordApp =nothing
3.1
''一。打开一个文档
Set WordDoc = getobject ('c:\a.txt')
Set WordApp = oWordDoc.Application
oWordApp.visible =true
msgbox '文档已经打开!'
3.2
'二。打开一个文档
'打开word应用程序
Set WordApp = createobject ('word.application')
oWordApp.visible=true
msgbox 'word应用程序被打开'
Set oWordDoc =oWordApp.Documents.open('c:\a.doc')
msgbox '文档已经打开'
oWordDoc.close
oWordApp.quit
Set WordDoc= nothing
Set WordApp =nothing
4:
'在word中写入内容
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:‘向一个文档中插入表格
EditWord 'c:\a.doc'
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
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
联系客服