Пришел сюда в поисках быстрого решения проблемы коллег, но не смог найти решения где-либо еще, поэтому выложил свое собственное решение для имитации TOC с использованием скрипта с PowerShell Version 5 на Win7 с Publisher 2010.
Содержание добавлено между страницами 2 и 3, где страница 1 была титульным листом, а страница 2 - страницей приветствия. Заголовок всегда был в форме текстового поля 1. Чтобы сохранить стиль выравнивания, для оглавления использовался моноширинный шрифт.
Если вы используете стандартные заголовки частей страницы издателя, то следующую переменную можно заменить переменной $ TOCAdd.
$Heading_Geometric_Title_Text = $Publisher.ActiveDocument.Pages($pge).Shapes(1).GroupItems[3].TextFrame.TextRange.Text $Heading_Geometric_Subtitle_Text = $Publisher.ActiveDocument.Pages($pge).Shapes(1).GroupItems[2].TextFrame.TextRange.Text $Heading_Brackets_Title_Text = $Publisher.ActiveDocument.Pages($pge).Shapes(1).GroupItems[2].TextFrame.TextRange.Text $Heading_Brackets_Subtitle_Text = $Publisher.ActiveDocument.Pages($pge).Shapes(1).GroupItems[5].TextFrame.TextRange.Text $Heading_Pure_Title_Text = $Publisher.ActiveDocument.Pages($pge).Shapes(1).GroupItems[3].TextFrame.TextRange.Text $Heading_Pure_Subtitle_Text = $Publisher.ActiveDocument.Pages($pge).Shapes(1).GroupItems[2].TextFrame.TextRange.Text
#
# Path to Publisher document $FileName = "$env:USERPROFILE\Documents\Publication_1.pub" $Publisher=NEW-Object –comobject Publisher.Application # Following line sets whether file is opened visible or not. $Publisher.ActiveWindow.visible = 1 # Open Publisher Document $Publisher.open($FileName) # Check how Many Pages in document $count = $Publisher.ActiveDocument.Pages.Count # How Many TOC Pages required in document for the amount of titles (assume 17 per page) $TOCPages = [math]::Round($count/17) # New Page Count including TOC required in document $countINC = $count += $TOCPages # Add TOC pages required insert Page(s) after last Pre page $Pre = 2 IF ($TOCPages -ge 1) { ForEach ( $n in 1..$TOCPages){ $Publisher.ActiveDocument.Pages.Add(1,$Pre) } # Add table on TOC Pages ForEach ( $n In 1..$TOCPages){ # Insert New Toc from this page No $n += $Pre # Insert table on the new TOC page with NumRows:=17, NumColumns:=3, Left:=50, Top:=70, Width:=600, Height:=300 $TOCPage = $Publisher.ActiveDocument.Pages($n).Shapes.AddTable( 17, 3, 50, 75, 600, 300) # Adjust column widths for table (shape) $TOCPage.Table.Columns(1).width = 30 #Item No $TOCPage.Table.Columns(2).width = 450 #Item $TOCPage.Table.Columns(3).width = 70 #Page No #Add Text Box Standard Text (type 1) adjust font size then make it Bold $TOCTitle = $Publisher.ActiveDocument.Pages($n).Shapes.AddTextbox(1, 360, 26, 150, 25) $TOCIndex = ($n -= ($Pre)) $TOCTitle.TextFrame.TextRange.Text = "Table of Contents $TOCIndex" $TOCTitle.TextFrame.TextRange.Font.Size = 14 $TOCTitle.TextFrame.TextRange.Font.Bold = 1 } } # Identify first page after TOC $start = $TOCPages += ($Pre += 1) # Create TOC Array (Fixed Content First) append to fixed length Char 46 (period) $TOCROW1 = 'Title Page' $TOCROW2 = 'Welcome to the Catalogue' $TOCROW3 = 'Table of Contents' $TOCArray = @() $TOCRow = "" | Select 'Table of Contents','PageNo' $TOCRow.'Table of Contents' = $TOCROW1.PadRight(60,[char]46) $TOCRow.PageNo = 1 $TOCArray = $TOCArray + $TOCRow $TOCRow = "" | Select 'Table of Contents','PageNo' $TOCRow.'Table of Contents' = $TOCROW2.PadRight(60,[char]46) $TOCRow.PageNo = 2 $TOCArray = $TOCArray + $TOCRow $TOCRow = "" | Select 'Table of Contents','PageNo' $TOCRow.'Table of Contents' = $TOCROW3.PadRight(60,[char]46) $TOCRow.PageNo = 3 $TOCArray = $TOCArray + $TOCRow # Use try / catch for page shapes returning error try { ForEach ($pge in $Start..$countINC){ IF ($pge -eq "" ){ Write-Host "Debug"} Else{ # Add TOC Array Entries $TOCAdd = $Publisher.ActiveDocument.Pages($pge).Shapes(1).TextFrame.TextRange.Text $TOCRow = "" | Select 'Table of Contents','PageNo'; $TOCRow.'Table of Contents' = $TOCAdd.PadRight(60,[char]46) $TOCRow.PageNo = $pge $TOCArray = $TOCArray + $TOCRow } } } catch { } # Debug uncomment following line to View Output of 'Table of Contents' based on the data added in Array variable 'TOCArray' # $TOCArray | Format-Table -Wrap -Property 'Table of Contents','PageNo' # Add TOC entries to TOC (uses monospaced font to get alignment) $n = 2 $q = -1 $Item =0 ForEach ($TOC in 1..$TOCPages) { $n++ $q++ $Row = 0 $Offset = ($q * $Range) $Range = 17 $TOCArray | Select-Object -Skip $Offset -First $Range -Property 'Table of Contents','PageNo'| ForEach-Object { $Row++ $Item++ $TOCEntry = $Publisher.ActiveDocument.Pages($n).Shapes(1).Table.Rows($Row).Cells(1); $TOCEntry.TextRange.Text = "$Item"; $TOCEntry.TextRange.Font.Size = 12; $TOCEntry.TextRange.Font.Bold = 1; $TOCEntry.TextRange.Font.Name = 'Consolas'; $TOCEntry = $Publisher.ActiveDocument.Pages($n).Shapes(1).Table.Rows($Row).Cells(2); $TOCEntry.TextRange.Text = $_.'Table of Contents'.replace("`r","").ToString() $TOCEntry.TextRange.Font.Size = 12; $TOCEntry.TextRange.Font.Bold = 1; $TOCEntry.TextRange.Font.Name = 'Consolas'; $TOCEntry = $Publisher.ActiveDocument.Pages($n).Shapes(1).Table.Rows($Row).Cells(3); #Need to Convert PageNo stored as System.Int32 to String $TOCEntry.TextRange.Text = $_.PageNo.ToString() $TOCEntry.TextRange.Font.Size = 12; $TOCEntry.TextRange.Font.Bold = 1; $TOCEntry.TextRange.Font.Name = 'Consolas'; } } # Save File (and increment versions), then close. $v++ $file = $Publisher.ActiveDocument.Name $BaseName = $file.Substring(0, $file.LastIndexOf('.')) $strFileName = $BaseName + "_v1" + $v $Publisher.ActiveDocument.SaveAs($strFileName) $Publisher.ActiveDocument.Close() $Publisher.quit() [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Publisher)