简体   繁体   中英

MS Word's range arithmetic in Powershell

I'm trying to automate adding new tables to a Word document using Powershell.

I wrote a Powershell script that is meant for adding summary tables based on a whole document in a proper location. It gathers the information from the file contents and then, in the selected ranges, creates a new summary tables. The table is always inserted at the end of the range (which is a chapter in the document). The range is based on the list headers. However, while adding a new table to the selected range, I cannot force Word to leave the next header which is chosen as the end of the range. It gets deleted.

For example: I'm having chapters from 1.1 to 1.10 in my file and I'm choosing to add a new table at the end of the chapter 1.1, right before the chapter 1.2. A whole chapter 1.2. header is deleted and the chapter 1.3 is now labeled as 1.2.

I tried substracting various numbers from the Range.End property, following information in the Microsoft documentation ( https://docs.microsoft.com/en-us/office/vba/api/word.range.end ), however is doesn't seem to give any results.

The code (shortcut):

Add-Type -AssemblyName Microsoft.Office.Interop.Word

$word = New-Object -ComObject Word.application
$report = $word.Documents.Open("C:\file.docx")

#information gathering here

#find the right location and add a new table
$start = $report.Paragraphs | ? {$_.Range.ListFormat.ListString -eq '1.1'} | % {$_.Range}
$end = $report.Paragraphs | ? {$_.Range.ListFormat.ListString -eq '1.2'} | % {$_.Range}
$first_table = $report.Range($start.Start, $end.Start).Tables.Add($end, 24, 4, [ref]$DefaultTableBehavior::wdWord9TableBehavior, [ref]$AutoFitBehavior::wdAutoFitFixed)

#continue with filling up the table

Part of the problem is that if you have a sequence of empty auto-numbered paragraphs, inserting a table into one of them will mean that subsequent paragraphs will be placed inside the table.

Also, if your 1.1 section can contain material, AFAICS your $start will contain the range of its first paragraph, which isn't very useful - all you really need is that location of the paragraph you want to insert before .

As an alternative, I suggest that you start by inserting a paragraph mark immediately before the 1.2 heading, then insert the table.

eg like this:

#$start = $report.Paragraphs | ? {$_.Range.ListFormat.ListString -eq '1.1'} | % {$_.Range}
$end = $report.Paragraphs | ? {$_.Range.ListFormat.ListString -eq '1.2'} | % {$_.Range}
$place = $report.Range($end.Start - 1, $end.Start-1)
$place.InsertParagraph()
$place = $report.Range($end.Start - 1, $end.Start-1)
$first_table = $place.Tables.Add($place, 24, 4, [ref]$DefaultTableBehavior::wdWord9TableBehavior, [ref]$AutoFitBehavior::wdAutoFitFixed)

If you don't want that extra paragraph mark, you can probably delete it, and if you really want, you could use

$report.Range($end.Start-1, $end.Start).Text = ""

after the table insertion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM