none
Remove Images from Table of Content RRS feed

  • Question

  • I programmatically add and update Table of Content in a Word document.

    Something strange has happened: The ToC contains images from the document among its lines.

    Any ideas?

    Thanks in advance!

    Thursday, February 6, 2014 10:22 AM

Answers

  • Thanks. I have solved the problem by non checking the "outline levels" option of TOC.

    Don't know what is its meaning, however, it did it!

           

    The "Outline levels" option, which is checked by default, means that Word brings into the TOC paragraphs whose "Outline level" (in the Paragraph dialog box) is set to something other than "Body Text." Usually you want to deselect it. If you want to modify an existing TOC, you can do so by displaying the field code (Alt+F9), clearing the \u switch, and then updating the TOC with F9. Press Alt+F9 again to hide the field codes.


    Stefan Blom, Microsoft Word MVP

    Sunday, February 9, 2014 3:04 PM
  • Thanks. I have solved the problem by non checking the "outline levels" option of TOC.

    Don't know what is its meaning, however, it did it!

    Thank you all for the help!

     
    Sunday, February 9, 2014 11:31 AM

All replies

  • Hello Tehila11,

    What code do you use for updating the ToC?

    What Word version do you have currently installed on the machine?

    Do you work with an open xml file format (*.docx) or binary one (*.doc)?

    Thursday, February 6, 2014 10:32 AM
  • Thanks for reply,

    I used Word 2010 and the extension of document is .doc or .rtf

    Here is my code:

    //Create a table of contents at the beginning of the document in case the document does not have already a self definition of TOC.
    	if(wordApp.ActiveDocument.TablesOfContents.Count == 0)
            {
    		TableOfContents toc = wordDoc.TablesOfContents.Add(rangeForTOCTOF, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    		wordDoc.TablesOfContents[1].TabLeader = WdTabLeader.wdTabLeaderDots;   
    		wordDoc.TablesOfContents.Format = WdTocFormat.wdTOCFormal;
    		toc.Update();
    		rangeForTOCTOF.InsertBefore("Table of Content\n");
           }//if
    					
    	//update table of contents
         wordApp.ActiveDocument.TablesOfContents[1].UpdatePageNumbers();

    Thanks!

    Thursday, February 6, 2014 10:44 AM
  • The problem isn't with how you've added the Table of Contents, but with the Styles of the paragraphs into which your images have been inserted. Evidently those paragraphs use one of the Heading Styles. You need to change those paragraphs' Styles to something else (eg Normal or Body Text), then update the Table of Contents.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, February 6, 2014 11:29 AM
  • Thanks for the idea,

    but what if I it sometimes all right and sometimes damaged and I use the same content (I insert to Word an existing document and then manipulate it).

    Thanks again.

    Thursday, February 6, 2014 11:40 AM
  • Hi Tehila11,

    First of all, I'd recommend releasing underlying COM objects instantly. For example:

    wordDoc.TablesOfContents.Add(

    The TableOfContents property of the Document class from the Word object model returns an instance of the corresponding class which should be released after. But then the Add method of the TableOfContents class is called. It returns a new TableOfContents object that represents a table of contents added to a document. The returned object is not released too.  

    Each time you call a property or method from the Word object model you get a reference counter increased for the underlying COM object.

    Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Word object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. 

    You can read more about this in the Systematically Releasing Objects article in MSDN. Though it is related to Outlook, the same can be applied to Word.

    Thursday, February 6, 2014 11:41 AM
  • It can only be "all right" if you intend to have the images in the Table of Contents! Fixing the paragraph Styles won't damage the document although, if the image is actually attached to a paragraph containing a heading, you'll need to separate them before changing the Paragraph Style.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, February 6, 2014 11:53 AM
  • Do you think that is the trigger to the current problem?

    Thanks.

    Thursday, February 6, 2014 11:54 AM
  • Most probably the issue is related to Styles as macropod already wrote. But I always recommend releasing underlying COM objects in .Net based applications (including VSTO add-ins). It will allow you avoid issues in future (or current issues).


    Thursday, February 6, 2014 12:01 PM
  • Thank you, Paul.

    What you meant about separating the images from the headings they are beneath?

    How exactly shall I change the image format?

    Thanks.

    Thursday, February 6, 2014 1:26 PM
  • What format do the images now have - inline, or something else? if they're inline and they're in a paragraph that contains heading text, you'll need to move them elsewhere (e.g. by inserting a return before the image, or by dragging it to a new location). If you insert a return before the image, you'll then need to make sure the paragraph containing the image is NOT in a heading Style.

    This is all fairly basic stuff...


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, February 6, 2014 3:26 PM
  • Thanks for reply,

    I tried to insert a break before and after the image. The break line format is NOT heading.

    Still, unfortunately, TOC contains pictures. What else have I to do?

    Sunday, February 9, 2014 8:34 AM
  • Did you update the TOC after making the changes?

    If yes, what Style does the image paragraph use and what is the TOC field code (you can see this by selecting the TOC and pressing Shift-F9)?


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, February 9, 2014 9:16 AM
  • Thanks. I have solved the problem by non checking the "outline levels" option of TOC.

    Don't know what is its meaning, however, it did it!

    Thank you all for the help!

     
    Sunday, February 9, 2014 11:31 AM
  • Thanks. I have solved the problem by non checking the "outline levels" option of TOC.

    Don't know what is its meaning, however, it did it!

           

    The "Outline levels" option, which is checked by default, means that Word brings into the TOC paragraphs whose "Outline level" (in the Paragraph dialog box) is set to something other than "Body Text." Usually you want to deselect it. If you want to modify an existing TOC, you can do so by displaying the field code (Alt+F9), clearing the \u switch, and then updating the TOC with F9. Press Alt+F9 again to hide the field codes.


    Stefan Blom, Microsoft Word MVP

    Sunday, February 9, 2014 3:04 PM