Solved

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

Posted on 2011-03-01
5
417 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now