Cheguei aqui procurando uma solução rápida para um problema de colegas e não consegui encontrar uma solução em outro lugar, então publiquei minha própria solução para simular o TOC usando script com o PowerShell Versão 5 no Win7 com o Publisher 2010.
TOC adicionado entre as páginas 2 e 3, onde a página 1 era uma página de rosto e 2 uma página de boas-vindas. O título era sempre em formato de caixa de texto 1. para manter o estilo de alinhamento, uma fonte monoespaçada era usada para TOC.
Se você estiver usando cabeçalhos de peça da página do editor padrão, o seguinte poderá ser a variável substituída $ 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)