Thanks in advance.
Stacy
Marco
"sthein5" <sth...@discussions.microsoft.com> wrote in message
news:FBA98F5F-7F09-4E98...@microsoft.com...
"Robert Robelo" wrote:
> Use the TablesOfContents.Add method
> http://msdn.microsoft.com/en-us/library/bb213383.aspx
>
> This code will add a TOC to an existing document with sections formatted
> with Heading1/Title and Headig2/Subtitle. Hope it helps :)
>
> # Microsoft.Office.Interop.Word.WdTabLeader
> $wdTabLeaderSpaces = 0
> $wdTabLeaderDots = 1
> $wdTabLeaderDashes = 2
> $wdTabLeaderLines = 3
> $wdTabLeaderHeavy = 4
> $wdTabLeaderMiddleDot = 5
>
> # Microsoft.Office.Interop.Word.WdSaveOptions
> $wdDoNotSaveChanges = 0
> $wdPromptToSaveChanges = -2
> $wdSaveChanges = -1
>
> # - - - - - - - - - - - - - #
>
> # document's full path
> $docPath = 'C:\Documents\MyDoc.docx'
>
> # instatiate an instance of Word
> $wd = New-Object -ComObject Word.Application -Property @{Visible = $true}
>
> # open your document
> $doc = $wd.Documents.Open($docPath)
>
> # argumets for the TablesOfContents.Add method
>
> # set position at the top of the document, start and end at char 0
> $range = $doc.Range([ref]0,[ref]0)
> $useHeadingStyles = $true
> $upperHeadingLevel = 1 # <-- Heading1 or Title
> $lowerHeadingLevel = 2 # <-- Heading2 or Subtitle
> $useFields = $false
> $tableID = $null
> $rightAlignPageNumbers = $true
> $includePageNumbers = $true
> # to include any other style set in the document add them here
> $addedStyles = $null
> $useHyperlinks = $true
> $hidePageNumbersInWeb = $true
> $useOutlineLevels = $true
>
> # add the TOC
> $toc = $doc.TablesOfContents.Add($range, $useHeadingStyles,
> $upperHeadingLevel, $lowerHeadingLevel, $useFields, $tableID,
> $rightAlignPageNumbers, $includePageNumbers, $addedStyles,
> $useHyperlinks, $hidePageNumbersInWeb, $useOutlineLevels)
>
> # to change the tab leader, set this to a
> # Microsoft.Office.Interop.Word.WdTabLeader
> $toc.TabLeader = $wdTabLeaderMiddleDot
>
> # eventually, close the document, pass a referenced
> # Microsoft.Office.Interop.Word.WdSaveOptions to the Close method
> # to save the doc as another file you can use the Document.SaveAs method
> # here changes are NOT saved
> $doc.Close([ref]$wdDoNotSaveChanges)
>
> # terminate Word
> $wd.Quit()
>
> --
> Robert