2

下面是代码:

$objWord = New-Object -Com Word.Application

$filename = 'C:\Chicago_NewUser.doc'
$objDocument = $objWord.Documents.Open($filename)

$LETable = $objDocument.Tables.Item(1)
$LETableCols = $LETable.Columns.Count
$LETableRows = $LETable.Rows.Count


Write-output "Starting to write... "

for($r=0; $r -le $LETableRows; $r++) {
    for($c=0; $c -le $LETableCols; $c++) {
        Write-host $r "x" $c
        $content = $LETable.Cell($r,$c).Range.Text
        Write-host $content
    }
}
$objDocument.Close()
$objWord.Quit()

该文档是 2003 Word 文档,带有一个表格 - 2 列和 3 行。但是打印的输出以第 3 行和第 1 列中的文本开始。

Word 文档可在此处获得:

https://docs.google.com/document/d/1W0usG4ASsvd3PqQ20l7P8rq0hWBsYlrI0iM1S5ZcumU/edit

4

1 回答 1

4

只需从 1 开始您的索引就可以了。我还在末尾添加了一行以正确关闭 winword。

$objWord = New-Object -Com Word.Application

$filename = 'c:\silogix\SILOGIX.doc'
$objDocument = $objWord.Documents.Open($filename)

$LETable = $objDocument.Tables.Item(1)
$LETableCols = $LETable.Columns.Count
$LETableRows = $LETable.Rows.Count


Write-output "Starting to write... "

for($r=1; $r -le $LETableRows; $r++) {
    for($c=1; $c -le $LETableCols; $c++) {
        Write-host $r "x" $c
        $content = $LETable.Cell($r,$c).Range.Text
        Write-host $content
    }
}
$objDocument.Close()
$objWord.Quit()
# Stop Winword Process
$rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objWord)
于 2012-07-06T04:06:06.480 回答