none
How to read Word's TableOfContent in C#? RRS feed

  • Question

  • Hi,

    Please help me on this :

    I have to read a word doc which has structure like this:

    table of content

    Heading1  - 2

    Heading2  - 3

    Heading3  - 4

     

    Heading1

    autoshape1

    ---------------------

    Heading2

    autoshape2

    ----------------

    How to read table of content and selecting respective autoshape

     

    Thanks for help!


    I am using C#, with VS 2008, .net 3.5
    Tuesday, May 10, 2011 9:39 AM

Answers

  • When you say the User will input the name of the shape, do you mean the name of the shape, or the text in the heading above it? Assuming it's the text in the heading (because, if not, it won't be in the TOC), it would be easier to do a Find in the document - just look for the input text in "Heading 2" Style and then make sure the one you have found is exactly as input (and doesn't just include the same text), and then grab the first Shape anchored to it - but be aware that that does not necessarily mean the shape that occurs immediately after it in the document flow (in your case you may know it to be correct, but in the general case it can't be guaranteed).
     

    Enjoy,
    Tony
    www.WordArticles.com
    Thursday, May 12, 2011 3:14 PM

All replies

  • Hello,

    Consider parsing the range returned by Document.TablesOfContents.Item(1).Range.


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Tuesday, May 10, 2011 1:01 PM
  • Hi Prasad

    I'm not sure I understand your question. What's the purpose of "reading the table of contents"? What should trigger selecting the autoshape? I think we need more detail. Please also include the version of Word involved.

    Note: As this is Word-specific question I'm going to move this discussion to the Word for Developers forum. There are a lot more people there who could help you.


    Cindy Meister, VSTO/Word MVP
    Tuesday, May 10, 2011 4:38 PM
    Moderator
  • Hi Cindy,

    Thanks for routing the question to word forum.

    Program should iterate through content table and compare each item with “input” string. If it matches, control should go to that point of the document and copy the auto shape.

     Wor version is 2007.

     

     


    • Edited by Prasad 2000 Wednesday, May 11, 2011 3:38 AM updated
    Wednesday, May 11, 2011 3:35 AM
  • It depends, to some extent, how complex your document and/or your TOC is - based on your example, it's hard to know. Do you want just autoshapes that immediately follow headings? Or just level 1 headings? Or just headings in the TOC? Or multiple shapes if they exist? Or only shapes if they exist without text? Do you want floating shapes that happen to be positioned after a heading even if they are anchored to a different paragraph? Do you want to update the TOC before you begin? etc., etc, ...
     

    Enjoy,
    Tony
    www.WordArticles.com
    Wednesday, May 11, 2011 9:38 AM
  • Hi Tony,

    Do you want just autoshapes that immediately follow headings?  - YES

    Under one heading only one autoshape will exist.

    Shape1

    <autoshape>

    Shape2

    <autoshape>

    shape3

    <autoshape>

    Wednesday, May 11, 2011 10:56 AM
  • I asked several representative questions, and you are still not providing anywhere near enough information to enable anyone to help you towards a solution. Are the shapes in-line or floating? Why do you need to navigate the TOC? Are some heading paragraphs not in the TOC?
     

    Enjoy,
    Tony
    www.WordArticles.com
    Wednesday, May 11, 2011 4:18 PM
  • Sorry Tony,

    I was busy with some workaround for this problem. Was rying to bookmark the shape and trying to copy it instead of this TOC.

    About your questions is concerened:

    Are the shapes in-line or floating?  - Floating

    Why do you need to navigate the TOC? - As per user input(name of the custom shape in word), function will check the TOC and go to the respective place and copy the shape. Here name of the shapes are in "heading 2" style.

    Are some heading paragraphs not in the TOC? - All the heading contents are populating in to TOC. I want when function should compare each item in TOC, and when its true control should move to the respective shape's position and copy it and return the shape.

    Thursday, May 12, 2011 1:12 PM
  • When you say the User will input the name of the shape, do you mean the name of the shape, or the text in the heading above it? Assuming it's the text in the heading (because, if not, it won't be in the TOC), it would be easier to do a Find in the document - just look for the input text in "Heading 2" Style and then make sure the one you have found is exactly as input (and doesn't just include the same text), and then grab the first Shape anchored to it - but be aware that that does not necessarily mean the shape that occurs immediately after it in the document flow (in your case you may know it to be correct, but in the general case it can't be guaranteed).
     

    Enjoy,
    Tony
    www.WordArticles.com
    Thursday, May 12, 2011 3:14 PM