Scripting >> Powershell >> Working with Microsoft Office Word document - Part 2

13.  Insert text into the cells of the first table in a document

$FilePath = "c:\temp\"
$FileName = "doc-with-bookmarks.docx"
$objWord = New-Object -ComObject Word.Application
$objWord.Visible = $True
$objDoc = $objWord.Documents.Open($FilePath + $FileName)

$Table1 = $objDoc.Tables.Item(1)
# Row 1 is heading
$Table1.Cell(2,1).Range.Text="Data @Row1 Col1"
$Table1.Cell(2,2).Range.Text="Data @Row1 Col2"
$Table1.Cell(2,3).Range.Text="Data @Row1 Col3"
$Table1.Cell(2,4).Range.Text="Data @Row1 Col4"
$Table1.Cell(2,5).Range.Text="Data @Row1 Col5"

14.  Adding rows to a table

$FilePath = "c:\temp\"
$FileName = "doc-with-bookmarks2.docx"
$objWord = New-Object -ComObject Word.Application
$objWord.Visible = $True
$objDoc = $objWord.Documents.Open($FilePath + $FileName)

$Table1 = $objDoc.Tables.Item(1)
# Row 1 is heading
$Table1.Cell(2,1).Range.Text="T1R1C1"
$Table1.Cell(2,2).Range.Text="T1R1C2"
$Table1.Cell(2,3).Range.Text="T1R1C3"
$Table1.Cell(2,4).Range.Text="T1R1C4"
$Table1.Cell(2,5).Range.Text="T1R1C5"
# add another row
$Table1.rows.add()
$Table1.Cell(3,1).Range.Text="T1R2C1"
$Table1.Cell(3,2).Range.Text="T1R2C2"
$Table1.Cell(3,3).Range.Text="T1R2C3"
$Table1.Cell(3,4).Range.Text="T1R2C4"
$Table1.Cell(3,5).Range.Text="T1R2C5"

15.  View the attachments in the document (InlineShapes OLE object)

$FileName = "c:\temp\mydocument.docx"
$objWord = New-Object -ComObject Word.Application
$objWord.Visible = $True
$objDoc = $objWord.Documents.Open($FileName)
$objSelection = $objWord.Selection
$objDoc.ActiveWindow.Left = 520
$objDoc.ActiveWindow.Width =640
$objDoc.ActiveWindow.Top = 0
$objDoc.ActiveWindow.Height = 620
$objDoc.InlineShapes | foreach {$_.OLEFormat} | out-gridview

16.  Jump to a specific page

$FilePath = "c:\temp\"
$FileName = "doc-with-bookmarks2.docx"
$objWord = New-Object -ComObject Word.Application
$objWord.Visible = $True
$objDoc = $objWord.Documents.Open($FilePath + $FileName)
$PageNo = "9"
# jump to page 9
$a = $objWord.Selection.Goto(1,1,8,$PageNo)