How do I keep a MSWord TOC from crashing a VB .NET Descendants search?
Posted on 2011-03-01
I have used the following line of code to search for sdtBlocks in the Body XML of a Word 2007 document:
Dim vblMilestone As SdtBlock = vptMainPart.Document.Body.Descendants(Of SdtBlock)().Where(Function(r) r.SdtProperties.GetFirstChild(Of Tag).Val = "milDetails").Single
This code works great as long as there is not a Table of Contents in the document before the sdtBlock I am searching for. If it finds a TOC first, it gives an Object error.
Obviously I can always add my TOC last, but I would like to already have it in the Template Document I am using. Can anyone help?