打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
QTP与word、excel、txt

读文件代码参考:

'强制打开变量声明开关

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','QTPword的操作'

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


6word中插入图片
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
        '
中间对齐
        oImg.Range.ParagraphFormat.Alignment = 1
        .TypeParagraph
        .TypeText 'qtp
学习之对word的基本操作'
        .TypeParagraph
   
    End with
   
   end function
7.
word中插入文本
EditWord 'C:\testbbo.doc', 'QTP
学习之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

 

 


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
关于word的VBA编程(转)
关于VB或VBA代码中前期绑定与后期绑定的区别
VB 打开Word文件
html在线显示word怎么在html中显示word
【代工案例002】批量发送Word工资条
Access,word,excel之间的数据传递
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服