I have to update the TOC of my document word automatically using powershell script , I used this script on my local machine and it works fine
$latestFile = Get-ChildItem -Path C:\ExportedDocuments -File -Filter "*.docx" | Sort-Object
LastAccessTime -Descending | Select-Object -First 1
$word = New-Object -ComObject Word.Application
$word.Visible=$true
$doc=$word.Documents.Open($latestFile.FullName)
$toc = $latestFile.TablesOfContents
$toc.Update()
$latestFile.save()
$latestFile.close()
When I tried to add a task on bamboo containing this script to update the TOC of my Word document, it doesn't work since the MS office is not installed under the bamboo server, it should be installed to use this script, but I'm not able to install it Is there any other solution to update/ Refresh my TOC without having the MS office installed?
Short answer:
In order to easily work with Word documents you need to have something installed to interpret/interact with the Word document.
e.g. If you are using the
Word.ApplicationCOM object, as above, you need to have Word/MS Office installed.Long answer:
If installing Office is out of the question, then the only other option would be to do things programmatically which is nowhere as easy as using the standard out-of-the-box MS Word COM object. You would have to use something like Open-XML-SDK to directly interact and manipulate the Open XML file itself. I would look at the WordprocesingML series by Eric White - specifically the Exploring Tables-of-Contents in Open XML WordprocessingML Documents. This has more information with respect to adding and updating a TOC.
Some additional help may be with this SO post: How to generate Table Of Contents using OpenXML SDK 2.0?