MS Word automation


Consider a few examples of automating MS Word.

The following code fragments in VBScript, you must copy the text box of  "Program" step.

Create a new document

Set objWord = CreateObject("Word.Application")

objWord.Visible = True

Set objDoc = objWord.Documents.Add()

 

Adding Rich Text

his example shows how to create a new MS Word document and add a text.

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 "Example of adding text"

objSelection.TypeParagraph()

objSelection.Font.Size = "14"

objSelection.TypeText "" & Date()

objSelection.TypeParagraph()

 

Adding a table to MS Word document

This example retrieves the service data and displays them in the table.

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