Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Table of Contents - Word Document

196 views
Skip to first unread message

sthein5

unread,
Feb 2, 2010, 3:15:01 PM2/2/10
to
I could use some help adding a table of contents to my word document from
PowerShell. What is some basic code for adding and formatting a table of
contents to a word document from PS?

Thanks in advance.

Stacy

Marco Shaw [MVP]

unread,
Feb 2, 2010, 7:24:26 PM2/2/10
to
Good question... What version of Word? 2007? You may be better off doing
it in raw XML. I'm not sure there's TOC functionality in VBA, but you could
check and prove me wrong... ;-)

Marco

"sthein5" <sth...@discussions.microsoft.com> wrote in message
news:FBA98F5F-7F09-4E98...@microsoft.com...

sthein5

unread,
Feb 25, 2010, 12:03:01 PM2/25/10
to

Thank you very much for the information. Have a great day.

"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

0 new messages