分享

QTP与word、excel、txt

 改变897 2016-06-01

读文件代码参考:

'强制打开变量声明开关

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

 

 


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多