We have a template document in which we have defined the TOC: ‘{TOC \o “1-3” \h \z \u}’.
After creating a document with the template file, if the content in the document does not contain ‘Heading 1- 3’ then the word adds the below comment in the Table of Content area: 'No table of contents entries found’
Now, we don’t want to show the above message in the document in case there is no Heading1-3.
For this now we have written the following code:
if (!HeaderExist(templateDocument))
{
RemoveTableOfContents(templateDocument);
templateDocument.UpdateFields();
}
private static bool HeaderExist(Document document)
{
NodeCollection paragraphs = document.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph paragraph in paragraphs)
{
switch (paragraph.ParagraphFormat.StyleIdentifier)
{
case StyleIdentifier.Heading1:
case StyleIdentifier.Heading2:
case StyleIdentifier.Heading3:
return true;
}
}
return false;
}
private static void RemoveTableOfContents(Document document)
{
foreach (Field field in document.Range.Fields)
{
if (field.Type == FieldType.FieldTOC)
{
field.Remove();
}
}
}
Is there any direct or easy way to check and remove the above message in the document?
image.png (11.3 KB)
Please help us to fix this issue.
Thanks & Regards,
MS