Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a Word paragraph is part of the Table Of Contents?

Tags:

c#

ms-word

How do I test if a paragraph is part of the Table Of Contents field?

Word.Application oWord = ....
doc = oWord.Documents.Open(....
foreach (Word.Paragraph p in doc.Paragraphs)
{
  bool pPartOfTOC = ???
  if(!pPartOfTOC){
    //do stuff if not in TOC
  }
}

What I'm trying to do is to parse all the paragraphs, skipping the ones that are part of the TOC. I was thinking to initially remove the TOC, but that would damage my pagination, and I need the page number also.

Any ideas?

like image 865
leoinfo Avatar asked Nov 14 '22 14:11

leoinfo


1 Answers

I think you can get the tables of content from the document. Each table of content will have a Range property and each paragraph will also have a Range property. You should be able to check for each paragraph whether or not it is fully contained within any of the ToC ranges.

Alternatively, you might simply be able to filter by paragraph styles (e.g. TOC 1).

like image 116
Matthew Strawbridge Avatar answered Nov 16 '22 02:11

Matthew Strawbridge