QTP -25 Working with MS Word 与word交互

来源:互联网 发布:东港世纪是什么软件 编辑:程序博客网 时间:2024/05/08 11:39

25Working with MS Word

Concept: Like most MS product, Word provide COM interface.

25.1 Word Automation Object Model

Application -->

Document-->

Bookmarks-->

Range

Range-->

Bookmarks

Selection-->

Bookmarks-->

Range

Document-->

Bookmarks

Range

Range-->

Bookmarks

 

Application: ref MS Word application;

Documents: a collection of all the document objects in word;

Document: a document object in word & a member ofDocuments;

Range: a contiguous area in a document with starting andending character position;

Selection: current selection in a window or pane. Only oneselection object in application. A selection can be either a selected area orinsertion point if nothing selected.

25.2 How to instantiate and terminate a word application?

Set oWordApp = CreateObject("Word.Application")

 

'Make the word application visible

oWordApp.Visible = True

 

MsgBox "Word Application instantiated! Click Ok to terminate word"

 

'Quit the word application

oWordApp.Quit

 

'Destroy the object reference

Set oWordApp = Nothing

 

If word is already open, get the reference of wordapplication:

Set oWordApp = GetObject(,"Word.Application")

 

'Make the word application visible

oWordApp.Visible = True

 

'Check the name of currently open word document

MsgBox oWordApp.ActiveDocument.Name

 

25.3 How to enumerate all the open word documents?

Set oWordApp = GetObject(,"Word.Application")

oWordApp.Visible = True

 

For Each oDoc In oWordApp.Documents

               'Print the file name and file path for each document

               MsgBox "Name: " & oDoc.Name & ", Path: " & oDoc.Path

Next

 

25.4 How to open and save a new document in word?

Set oWordDoc = oWordApp.Documents.Add

oWordDoc.SaveAs "C:\Test.docx"

oWordDoc.Close

 

25.5 How to get reference already existing word document?

‘Method 1

Set oWordDoc = GetObject("C:\Test.doc")

 

'Since we have got the refernce to from this

'document object to get refernce to the word application object

Set oWordApp = oWordDoc.Application

 

'Make the word application visible

oWordApp.Visible = True

‘Method 2

Set oWordApp = CreateObject("Word.Application")

 

'Make the word application visible

oWordApp.Visible = True

 

'Open a existing document

Set oWordDoc = oWordApp.Documents.Open ("C:\Test.doc")

 

25.6 How to open a word in the read only mode?

参数里面的设置

Set oWordDoc = oWordApp.Documents.Open("C:\Test.doc",,True)

 

Open 的 所有参数

'Function Open(ByVal FileName, [ByVal ConfirmConversions],

'[ByVal ReadOnly], [ByVal AddToRecentFiles], [ByVal PasswordDocument],

'[ByVal PasswordTemplate], [ByVal Revert], [ByVal WritePasswordDocument],

'[ByVal WritePasswordTemplate], [ByVal Format], [ByVal Encoding],

'[ByVal Visible], [ByVal OpenAndRepair], [ByVal DocumentDirection],

'[ByVal NoEncodingDialog], [ByVal XMLTransform]) As Document

25.7 How to print an open word document?

oWordDoc.PrintOut

 

25.8 How to insert text into word from a pre-defined style?

Selection object 的style 属性和TypeText方法:

'Select the whole word document

oWordDoc.Range.Select

 

'Get the selection object from the application object

Set oWordSel = oWordApp.Selection

 

With oWordSel

               'Type some text with style as /Heading 1/

               .Style = "Heading 1"

               .TypeText "This is Heading 1"

               .TypeParagraph

              

               'Type some text with style as /Heading 2/

               .Style = "Heading 2"

               .TypeText "This is Heading 2"

               .TypeParagraph

End With

 

25.9 How to inset image into word and scale its size by 50%?

With oWordSel

'Insert the image and get the object reference to the inserted image

               Set oImg = .InlineShapes.AddPicture ("C:\DesktopImage.bmp", False, True)

              

               'Scale the image to 50% of the existing size

               oImg.Width = oImg.Width * 0.50

               oImg.Height = oImg.Height * 0.50

              

               'Center allignment of the paragraph

               Const wdAlignParagraphCenter = 1

              

               'Allign the image in the center of the page

               oImg.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

 

               'Type a small caption below the image    

               .TypeParagraph

               .TypeText "This is the desktop capture scaled to 50%"

End With

 

25.10 How to find and replace some text in worddocument?

Sample code: 把document中的”%AUTHOR_NAME%”2替换为"TarunLalwani"

'Select the whole word document

oWordDoc.Range.Select

 

'Get the selection object from the application object

Set oWordSel = oWordApp.Selection

 

With oWordSel

               .TypeText "Author name for this document is: %AUTHOR_NAME%"

               .TypeParagraph

              

               'Find the %AUTHOR_NAME% document in the word file and replace

               'the same with the actual value

              

               'Need to take the range from whole document since current selection

               'does not have the typed text

               With oWordDoc.Range.Find

                              .Text = "%AUTHOR_NAME%"

                              '.MatchCase = False

                              '.MatchWholeWord = True

                             

                              .Replacement.Text = "Tarun Lalwani"

              

                              Const wdReplaceAll = 2

                              Const wdReplaceNone = 0

                              Const wdReplaceOne = 1

              

                              'Set the value of Replace as wdReplaceAll

                              .Execute ,,,,,,,,,,wdReplaceAll     

               End with

End With

 

Execute 方法的所有参数:

                              'Function Execute([FindText], [MatchCase], [MatchWholeWord],

                              '[MatchWildcards], [MatchSoundsLike], [MatchAllWordForms],

                              '[Forward], [Wrap], [Format], [ReplaceWith], [Replace],

                              '[MatchKashida], [MatchDiacritics], [MatchAlefHamza], [MatchControl]) As

                              ' Boolean

Note: 在项目中,把templatedoc 的configurable/updatablevalues to be written between special characters (%% above), 之后再把actual value 代替,最后另存文档.

25.11 How to insert a table into a word document?

oWordDoc.Range.Select

 

'Get the selection object from the application object

Set oWordSel = oWordApp.Selection

 

With oWordSel

               'Add a new table and get the object reference to the newly added

               'table. Table size: 5 rows X 3 columns

               Set oNewTable = .Tables.Add(.range,5,3)

              

               'Set the font and style for the table

               oNewTable.Range.Font.Size = 8

               oNewTable.Range.Style = "Medium Grid 3"

              

               i = 1

               'Add the header rows

               oNewTable.Cell(i, 1).Range.Text = "i"

               oNewTable.Cell(i, 2).Range.Text = "i * 2"

               oNewTable.Cell(i, 3).Range.Text = "i * 3"

              

               'Fill the data for rest of the rows

               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

              

               'Add one more row to the table

               oNewTable.Rows.Add

              

               'Add the details to the last row

               i = oNewTable.Rows.Count

               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

              

End With

 

25.12 How to change the font for the text being written in a word document?

oWordDoc.Range.Select

 

'Get the selection object from the application object

Set oWordSel = oWordApp.Selection

 

With oWordSel

               'Change the font to Arial (10)

               .Font.Name = "Arial"

               .Font.Size = 10

              

               .TypeText "This is in Arial 10 point size"

               .TypeParagraph

End With

 

25.13 How to preserve the text already present in a word document while writing?

oWordDoc.Range.Select

'Get the selection object from the application object

Set oWordSel = oWordApp.Selection

 

With oWordSel

               Const wdStory = 6

               Const wdMove = 0

              

               'Move to the end of the document

               .EndKey wdStory , wdMove

              

               .TypeText "This is in text ADDED AT LAST"

               .TypeParagraph

End With


 

原创粉丝点击