Pages

Thursday 3 November 2011

Microsoft Word Scripts in QTP

Add Formatted Text to a Word Document


Demonstration script that displays formatted data in a Microsoft Word document.
Set objWord = CreateObject("Word.Application")

objWord.Visible = True
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection

objSelection.Font.Name = "Arial"
objSelection.Font.Size = "18"
objSelection.TypeText "Network Adapter Report"
objSelection.TypeParagraph()

objSelection.Font.Size = "14"
objSelection.TypeText "" & Date()
objSelection.TypeParagraph()
 

Add a Formatted Table to a Word Document


Demonstration script that retrieves service data from a computer and then displays that data in a formatted table in Microsoft Word.
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()

Set objRange = objDoc.Range()
objDoc.Tables.Add objRange,1,3
Set objTable = objDoc.Tables(1)

x=1

strComputer = "."
Set objWMIService = _
    GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")

For Each objItem in colItems
    If x > 1 Then
        objTable.Rows.Add()
    End If
    objTable.Cell(x, 1).Range.Font.Bold = True
    objTable.Cell(x, 1).Range.Text = objItem.Name
    objTable.Cell(x, 2).Range.text = objItem.DisplayName
    objTable.Cell(x, 3).Range.text = objItem.State
    x = x + 1
Next
 

Apply a Style to a Table in a Word Document


Demonstration script that retrieves service data from a computer, displays that data in a table in Microsoft Word, then formats the data by using a predefined Microsoft Word style
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()

Set objRange = objDoc.Range()
objDoc.Tables.Add objRange,1,3
Set objTable = objDoc.Tables(1)
objTable.Range.Font.Size = 10
objTable.Range.Style = "Table Contemporary"

x=2

objTable.Cell(x, 1).Range.Text = "Service Name"
objTable.Cell(x, 2).Range.text = "Display Name"
objTable.Cell(x, 3).Range.text = "State"

strComputer = "."
Set objWMIService = _
    GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")

For Each objItem in colItems
    If x > 1 Then
        objTable.Rows.Add()
    End If
    objTable.Cell(x, 1).Range.Text = objItem.Name
    objTable.Cell(x, 2).Range.text = objItem.DisplayName
    objTable.Cell(x, 3).Range.text = objItem.State
    x = x + 1
Next
 

Add a Table to a Word Document


Demonstration script that retrieves service information from a computer and then displays that information in tabular format in Microsoft Word.
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()

Set objRange = objDoc.Range()
objDoc.Tables.Add objRange,1,3
Set objTable = objDoc.Tables(1)

x=1

strComputer = "."
Set objWMIService = _
    GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")

For Each objItem in colItems
    If x > 1 Then
        objTable.Rows.Add()
    End If
    objTable.Cell(x, 1).Range.Text = objItem.Name
    objTable.Cell(x, 2).Range.text = objItem.DisplayName
    objTable.Cell(x, 3).Range.text = objItem.State
    x = x + 1
Next
 

Append Text to a Word Document


Demonstration script that appends the current date to the existing Microsoft Word document C:\Scripts\Word\Testdoc.doc.
Const END_OF_STORY = 6
Const MOVE_SELECTION = 0

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Open("c:\scripts\word\testdoc.doc")
Set objSelection = objWord.Selection
objSelection.EndKey END_OF_STORY, MOVE_SELECTION

objSelection.TypeParagraph()
objSelection.TypeParagraph()

objSelection.Font.Size = "14"
objSelection.TypeText "" & Date()
objSelection.TypeParagraph()
objSelection.TypeParagraph()

objSelection.Font.Size = "10"
 

Create a New Word Document


Demonstration script that creates and displays a new Microsoft Word document.
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()
 

Create and Save a Word Document


Demonstration script that retrieves network adapter data from a computer, displays that data in a Microsoft Word document, and then saves the document as C:\Scripts\Word\Testdoc.doc.
Set objWord = CreateObject("Word.Application")
objWord.Caption = "Test Caption"
objWord.Visible = True

Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection

objSelection.Font.Name = "Arial"
objSelection.Font.Size = "18"
objSelection.TypeText "Network Adapter Report"
objSelection.TypeParagraph()

objSelection.Font.Size = "14"
objSelection.TypeText "" & Date()
objSelection.TypeParagraph()
objSelection.TypeParagraph()

objSelection.Font.Size = "10"

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration")

For Each objItem in colItems

    objSelection.Font.Bold = True
    objSelection.TypeText "ARP Always Source Route: " 
    objSelection.Font.Bold = False
    objSelection.TypeText "" & objItem.ArpAlwaysSourceRoute
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "ARP Use EtherSNAP: "
    objSelection.Font.Bold = False
    objSelection.TypeText ""  & objItem.ArpUseEtherSNAP
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "Caption: "
    objSelection.Font.Bold = False
    objSelection.TypeText ""  & objItem.Caption
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "Database Path: "
    objSelection.Font.Bold = False
    objSelection.TypeText ""   & objItem.DatabasePath
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "Dead GW Detection Enabled: "
    objSelection.Font.Bold = False
    objSelection.TypeText ""   & objItem.DeadGWDetectEnabled
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "Default IP Gateway: " 
    objSelection.Font.Bold = False
    objSelection.TypeText "" & objItem.DefaultIPGateway
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "Default TOS: "
    objSelection.Font.Bold = False
    objSelection.TypeText ""  & objItem.DefaultTOS
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "Default TTL: "
    objSelection.Font.Bold = False
    objSelection.TypeText ""  & objItem.DefaultTTL
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "Description: "
    objSelection.Font.Bold = True
    objSelection.Font.Bold = False
    objSelection.TypeText ""  & objItem.Description
    objSelection.TypeParagraph()

    objSelection.TypeParagraph()

Next

objDoc.SaveAs("C:\Scripts\Word\testdoc.doc")
objWord.Quit
 

Display Service Information in a Word Document


Demonstration script that retrieves service information from a computer and then displays that data in a Microsoft Word document.
Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection

objSelection.TypeText "Services Report"
objSelection.TypeParagraph()
objSelection.TypeText "" & Now
objSelection.TypeParagraph()
objSelection.TypeParagraph()

strComputer = "."
Set objWMIService = _
    GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")

For Each objItem in colItems
    objSelection.TypeText objItem.DisplayName & " -- " & objItem.State
    objSelection.TypeParagraph()
Next
 

List Microsoft Word Properties


Demonstration script that lists Microsoft Word configuration settings.
On Error Resume Next

Set objWord = CreateObject("Word.Application")
Wscript.Echo "Active Printer:", objWord.ActivePrinter

For Each objAddIn in objWord.AddIns
    Wscript.Echo "AddIn: ", objAddIn
Next

Wscript.Echo "Application:", objWord.Application
Wscript.Echo "Assistant:", objWord.Assistant

For Each objCaption in objWord.AutoCaptions
    Wscript.Echo "AutoCaptions:", objCaption
Next
Wscript.Echo "Automation Security:", objWord.AutomationSecurity
Wscript.Echo "Background Printing Status:", objWord.BackgroundPrintingStatus
Wscript.Echo "Background Saving Status:", objWord.BackgroundSavingStatus
Wscript.Echo "Browse Extra File Type:", objWord.BrowseExtraFileTypes
Wscript.Echo "Build:", objWord.Build
Wscript.Echo "Caps Lock:", objWord.CapsLock
Wscript.Echo "Caption:", objWord.Caption

For Each objLabel in objWord.CaptionLabels
    Wscript.Echo "Caption Label:", objLabel
Next

Wscript.Echo "Check Language:", objWord.CheckLanguage

For Each objAddIn in objWord.COMAddIns
    Wscript.Echo "COM AddIn:", objAddIn
Next

Wscript.Echo "Creator:", objWord.Creator

For Each objDictionary in objWord.CustomDictionaries
    Wscript.Echo "Custom Dictionary:", objDictionary
Next

Wscript.Echo "Customization Context:", objWord.CustomizationContext
Wscript.Echo "Default Legal Blackline:", objWord.DefaultLegalBlackline
Wscript.Echo "Default Save Format:", objWord.DefaultSaveFormat
Wscript.Echo "Default Table Separator:", objWord.DefaultTableSeparator

For Each objDialog in objWord.Dialogs
    Wscript.Echo "Dialog:", objDialog
Next

Wscript.Echo "Display Alerts:", objWord.DisplayAlerts
Wscript.Echo "Display Recent Files:", objWord.DisplayRecentFiles
Wscript.Echo "Display Screen Tips:", objWord.DisplayScreenTips
Wscript.Echo "Display Scroll Bars:", objWord.DisplayScrollBars

For Each objDocument in objWord.Documents
    Wscript.Echo "Document:", objDocument
Next

Wscript.Echo "Email Template:", objWord.EmailTemplate
Wscript.Echo "Enable Cancel Key:", objWord.EnableCancelKey
Wscript.Echo "Feature Install:", objWord.FeatureInstall

For Each objConverter in objWord.FileConverters
    Wscript.Echo "File Converter:", objConverter
Next

Wscript.Echo "Focus In MailHeader:", objWord.FocusInMailHeader

For Each objFont in objWord.FontNames
    Wscript.Echo "Font Name:", objFont
Next

Wscript.Echo "Height", objWord.Height

For Each objBinding in objWord.KeyBindings
    Wscript.Echo "Key Binding:", objBinding
Next

For Each objFont in objWord.LandscapeFontNames
    Wscript.Echo "Landscape Font Name:", objFont
Next

Wscript.Echo "Language", objWord.Language

For Each objLanguage in objWord.Languages
    Wscript.Echo "Language:", objLanguage
Next

Wscript.Echo "Left", objWord.Left
Wscript.Echo "Mail System:", objWord.MailSystem
Wscript.Echo "MAPI Available:", objWord.MAPIAvailable
Wscript.Echo "Math Coprocessor Available:", objWord.MathCoprocessorAvailable
Wscript.Echo "Mouse Available:", objWord.MouseAvailable
Wscript.Echo "Name:", objWord.Name
Wscript.Echo "Normal Template:", objWord.NormalTemplate
Wscript.Echo "Num Lock:", objWord.NumLock
Wscript.Echo "Parent:", objWord.Parent
Wscript.Echo "Path:", objWord.Path
Wscript.Echo "Path Separator:", objWord.PathSeparator
Wscript.Echo "Print Preview:", objWord.PrintPreview

For Each objFile in objWord.RecentFiles
    Wscript.Echo "Recent File:", objFile
Next

Wscript.Echo "Screen Updating:", objWord.ScreenUpdating
Wscript.Echo "Show Visual Basic Editor:", objWord.ShowVisualBasicEditor
Wscript.Echo "Special Mode:", objWord.SpecialMode
Wscript.Echo "Startup Path:", objWord.StartupPath

For Each objTask in objWord.Tasks
    Wscript.Echo "Task:", objTask
Next

For Each objTemplate in objWord.Templates
    Wscript.Echo "Template:", objTemplate
Next

Wscript.Echo "Top:", objWord.Top
Wscript.Echo "Usable Height:", objWord.UsableHeight
Wscript.Echo "Usable Width:", objWord.UsableWidth
Wscript.Echo "User Address:", objWord.UserAddress
Wscript.Echo "User Control:", objWord.UserControl
Wscript.Echo "User Initials:", objWord.UserInitials
Wscript.Echo "User Name:", objWord.UserName
Wscript.Echo "Version:", objWord.Version
Wscript.Echo "Visible:", objWord.Visible
Wscript.Echo "Width:", objWord.Width

For Each objWindow in objWord.Windows
    Wscript.Echo "Window:", objWindow
Next

Wscript.Echo "Window State:", objWord.WindowState
objWord.Quit
 

Modify Bookmark Text in a Word Document


Demonstration script that changes the text of two different bookmarks in an existing Microsoft Word document.
Set objWord = CreateObject("Word.Application")
objWord.Caption = "Test Caption"
objWord.Visible = True

Set objDoc = objWord.Documents.Open("c:\scripts\word\bookmarkdoc.doc")

Set objRange = objDoc.Bookmarks("NameBookmark").Range
objRange.Text = "Bob"

Set objRange = objDoc.Bookmarks("AddressBookmark").Range
objRange.Text = "999"
 

Open and Print a Word Document


Demonstration script that opens and prints and existing Microsoft Word document.
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("c:\scripts\inventory.doc")

objDoc.PrintOut()
objWord.Quit
 

Read a Bookmark in a Word Document


Demonstration script that retrieves the values of two different Microsoft Word bookmarks.
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("c:\scripts\word\bookmarkdoc.doc")
Set objRange = objDoc.Bookmarks("NameBookmark").Range

Wscript.Echo objRange.Text 

Set objRange = objDoc.Bookmarks("AddressBookmark").Range
Wscript.Echo objRange.Text 

objWord.Quit
 

Use Word to Search for Files


Demonstration script that uses Microsoft Word to locate all the .mp3 files stored on drive C of the local computer.
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()

objWord.FileSearch.FileName = "*.mp3"
objWord.FileSearch.LookIn = "C:\"
objWord.FileSearch.SearchSubfolders = True
objWord.FileSearch.Execute

For Each objFile in objWord.FileSearch.FoundFiles
    Wscript.Echo objFile
Next

objWord.Quit
 

2 comments:

  1. a nice piece of codes for word using VB scrit.i have a doubt?
    should we use the window management for the word
    is there any alternative code for the table formatting,, adding data to tables in word.
    thanks for posting this type of special codes..

    ReplyDelete