Solved

How do I keep a MSWord TOC from crashing a VB .NET Descendants search?

Posted on 2011-03-01
5
425 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:jdklem
  • 3
  • 2
5 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35021116
>If it finds a TOC first, it gives an Object error.
What is the exact error? Is it due to the ".Single" call?
0
 

Author Comment

by:jdklem
ID: 35021912
The error message says, "Object reference not set to an instance of an object".

I also tried using ".FirstOrDefault".

Is there anything else I can try?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 35021962
It looks like the

GetFirstChild(Of Tag)

is returning nothing.
0
 

Author Comment

by:jdklem
ID: 35022120
I put the TOC in a Content Control with a tag name.
I verified in the document.xml that the Tag child with a Val was there, but it still crashed with the same error.
0
 

Author Closing Comment

by:jdklem
ID: 35022599
Your GetFirstChild(Of Tag) led me to look more closely into the TOC XML.  I found the TOC is a sdtBlock without a Tag.  I manually added the Tag property to the sdtBlock, and now it works.

Putting the TOC in a Content Control didn't work because that just put the sdtBlock inside an sdtBlock and buried the problem deeper.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Configure a Bean in an XML file 4 33
Adding Existing Item to vb.net Project causes Errors 4 33
Eliminate additional border 1 18
Sending receiving text messages in vb.net 15 38
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question