none
i'm automating ms word 2007 using c# RRS feed

  • Question

  • insert table in word automation and generate list of tables
    how can i programmatically add a table in the word document where the number of columns and rows,table title and the data will be specified by the user in the C# windows form application.
    How can I track the page number where the table falls in the word document?
    Then, the table title and page number will be use for automatic generation of a list of all the tables inserted in the word document.
    How can these be done? Do I need a database for this? I don't know how to start. Thanks in advance
    Tuesday, January 4, 2011 12:01 AM

Answers

  • Hi hmFG

    You might want to read through this article for the basic code on creating tables in Word

    http://msdn.microsoft.com/en-us/library/aa537149(office.11).aspx

    As for the list of tables, Word can generate this automatically in the document if you're inserting Captions for the tables. It sounds as if you would be... "contain the table number, table title and the page number"

    In Word 2007, as an end-user, create a document with a couple of tables. Click in one table, go to the References tab, click Insert Caption, make sure it's suggestion a caption for the table and enter some text. Repeat for the other table(s).

    Now go to the top or end of the document and click "Insert table of figures". Make sure it's suggesting the same label used for the captions (Table). Make sure "Use hyperlinks instead of page numbers" is NOT activated. Is this basically what you need when you say "list of tables"?


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by hmFG Wednesday, January 12, 2011 12:09 AM
    Tuesday, January 11, 2011 6:34 PM
    Moderator

All replies

  • Hi hmFG,

    Thank you for pasting in the MSDN Forum!

    You can customize a table in the Word document. I would recommend you follow these steps:

    1. Get a range object from your word document.
    2. If you want to add a title above the table, use the method "InsertBefor(string)" on the range object.
    3. Get a range of the document where you want to place the table.
    4. Use the method Add from the document property "Tables", and define the number of rows and columns you would like to have. After doing this you will have inserted a table into the word document. But you will never see the border of the tale. If you want to see it, perform step5.
    5. Define an object to set table style. Use the method "set_style" of Word. Table object to set the style of this table.
    6. If you want to fill data into the table, you can use the method "cell(int,int)" to get a cell. After you get the cell, you can add something into the range of this cell.
    7. You can give page number from the range object property "Information" by use the enumeration "Word.WdInformation.wdNumberOfPagesInDocument".

    You can reference this document for more information about what I have told.

    http://msdn.microsoft.com/en-us/library/w1702h4a.aspx

    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/706dfdef-39c2-445b-9000-7205c30e879a

    What do you mean "Then the table title and page number will be use for automatic generation of a list of all the tables inserted in word document." Would you please give me more detils for further research?

    • Proposed as answer by AS Leaf Tuesday, January 11, 2011 8:11 AM
    • Marked as answer by hmFG Tuesday, January 11, 2011 5:59 PM
    • Unmarked as answer by Cindy Meister MVPModerator Tuesday, January 11, 2011 6:26 PM
    Thursday, January 6, 2011 10:02 AM
  • Hi again hmFG,

     

    Did you solve your issue? I am writing to check the status of this thread. Would you please let me know if the suggestion above works for you? If you have any doubt or concerns about this, please unmark it back.

     

    Best Regards,

    Tuesday, January 11, 2011 8:54 AM
  • i'm not alerted that someone already responded to this post

    I've just checked this post now. i'm sorry

    Thank you for your response, i'm a newbie in c# pogramming i can't figure out how i can carry out the steps you share but I know it would be a great help if i would understand them thoroughly, Can i have some code snippets for each  steps?

    "the table title and page number will be use for automatic generation of a list of all the tables inserted in word document"

    -I mean, aside from the task of inserting table in the word document using the c# windows form interface, I am also asked to create a "List of Tables" this is the list of all the tables inserted in the word document, the List of Tables would contain the table number, table title and the page number that the table falls in the word document

    Thanks a lot XuYang_790613

    I would check this post from time to time. I'm running out of time in doing my project, Thank you so much for your response.

    Tuesday, January 11, 2011 5:59 PM
  • Hi hmFG

    You might want to read through this article for the basic code on creating tables in Word

    http://msdn.microsoft.com/en-us/library/aa537149(office.11).aspx

    As for the list of tables, Word can generate this automatically in the document if you're inserting Captions for the tables. It sounds as if you would be... "contain the table number, table title and the page number"

    In Word 2007, as an end-user, create a document with a couple of tables. Click in one table, go to the References tab, click Insert Caption, make sure it's suggestion a caption for the table and enter some text. Repeat for the other table(s).

    Now go to the top or end of the document and click "Insert table of figures". Make sure it's suggesting the same label used for the captions (Table). Make sure "Use hyperlinks instead of page numbers" is NOT activated. Is this basically what you need when you say "list of tables"?


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by hmFG Wednesday, January 12, 2011 12:09 AM
    Tuesday, January 11, 2011 6:34 PM
    Moderator
  • Ma'am Cindy, thank you for the response.

    How can I programmatically create the list of tables?

    how can i edit the tables inserted in the word document using my c# windows form application?

    After the generation of the Microsoft word document, I want the user to edit his work on my c# windows form application and not on the MSWord file. Do i need a database for this?

    Thank you ma'am

     

    Wednesday, January 12, 2011 9:58 PM
  • Hi hmFG

    I split this off yesterday, already, because it's a completely new question. Please follow-up in that message thread

    http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/37b42c9f-826a-48dc-aaab-2b52a5868e50


    Cindy Meister, VSTO/Word MVP
    Thursday, January 13, 2011 8:36 AM
    Moderator