none
How to update information in a Word document RRS feed

  • Question

  • Ma'am Cindy, how can i use again my C# windows form application when the user wants to update the previously generated microsoft word document?

    after the user finish generating the doc file, how can i update the inserted table data, table structures, caption, table number and the list of tables in the generated Microsoft word document using my C# application? will i use a database? so that in the next time the user would edit his work, he will still working in my c# application?

    I'm sorry if I'm not making my questions clear Ma'am.

    Thank you so much for your response

    • Split by Cindy Meister MVPModerator Wednesday, January 12, 2011 12:54 PM split from answered question for better visibility http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/3c2b5f7e-f049-4ddf-ab0f-318889e1b2f7
    Wednesday, January 12, 2011 12:28 AM

Answers

  • Hi hmFG

    <<Will i still be using the richtextbox in my c# application for the text input of the user?>>

    If we're talking about the suggested alternate approach of using OpenXML, the answer is YES.

    <<-how will i do this? is it with the use of the open xml file format ma'am?

    Can you give me sample codes for putting the things into Word? =)>>

    For more assistance on the OpenXML approach you need to go to OpenXMLDeveloper.org and the Open XML SDK forum. I cannot help you with the details of how this works. I only know that it's possible. (I'm an interop object model specialist - can't be good at everything :-))

    <<<<Word.Table tbl = doc.Bookmarks.get_Item("test").Range.Tables[1];>>

    -this code is for? I'm sorry ma'am, how about the insertion of tables using my c# application? how will it be done?>>

    How tables can be inserted in Word using the Interop was settled in the other discussion, with the link to that MSDN article. Your follow-up question was how to update an existing table. I asked you if you knew how to identify things in a Word document and you answered you did not. In my last reply, I then described how to work with bookmarks in the UI.

    This code snippet shows how to get the table object within a bookmark using C# code. But you have to go through all the steps I outline in order to understand what's going on. I don't write out all that text for no reason...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bessie Zhao Wednesday, January 19, 2011 3:56 AM
    Sunday, January 16, 2011 8:33 AM
    Moderator

All replies

  • Hi hmFG

    Since we don't really know your application in detail and how the document will be used, it's difficult to offer much advice.

    Ca you tell us

    1. The version of Word you intend to support

    2. What kinds of changes the user might want to make to the table structure

    3. For what the document will be used, after it's been generated?

    4. Does you application do anything else to this document besides inserting and maintaining this table?


    Cindy Meister, VSTO/Word MVP
    Wednesday, January 12, 2011 1:19 PM
    Moderator
  • 1. The version of Word you intend to support

    -I'm using MSWord 2007

    2. What kinds of changes the user might want to make to the table structure

    -The user might want to change the data in the table, add new rows, change the table number and caption

    3.For what the document will be used, after it's been generated?

    -for printing ma'am, I'm sorry ma'am i don't know if i correctly answered this question

    4. Does you application do anything else to this document besides inserting and maintaining this table?

    - it would also insert texts, figures, insert images in the header and footer, manage page numbers

    Thank you so much ma'am for your help

    Thursday, January 13, 2011 9:45 AM
  • Hi hmFG

    Thank you for the additional information :-) Printing was exactly what I wanted to know for (3). That tells me that, basically, the user isn't going to be working directly in the document? So there's no chance he's going to be deleting and moving things?

    Since you're targeting Word 2007, I feel I should remark that it would probably be more efficient for you to be doing the document manipulation using the Office Open XML file format, rather than working over the interop. For more information on that, see OpenXMLDeveloper.org. There's also a Open XML SDK forum on MSDN that supports that.

    So, basically what you're looking for is a way to identify the table in the document? Are you familiar with using bookmarks to find things in a Word document? Or working with content controls?


    Cindy Meister, VSTO/Word MVP
    Thursday, January 13, 2011 11:29 AM
    Moderator
  • Yes ma'am the user is not going to work directly in the document. I want them to work in my c# windows form application. For more information ma'am, i am using this article as the foundation of my project: http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx

    in addition to that, i'm using richtextbox to get the user input, i'm having problems on the tables and figures.

     

    "more efficient for you to be doing the document manipulation using the Office Open XML file format, rather than working over the interop"

    --will i be restarting my project when i use the office open xml file format? can i still use my c# windows form application? I'm sorry ma'am, I hope I'm making my questions clear to you

     

    "So, basically what you're looking for is a way to identify the table in the document? Are you familiar with using bookmarks to find things in a Word document? Or working with content controls?"

    --yes i want to keep track with the tables in the document, i'm not much familiar with bookmarks and content controls ma'am

     

    thank you very much ma'am for your help. please be patient with me

    Thursday, January 13, 2011 1:19 PM
  • Hi hmFG

    <<please be patient with me>>

    Thank you for being patient with all my questions :-) With something complex there's often no single, simple answer, so it helps to get a feel for the entire project.

    << i'm using richtextbox to get the user input, i'm having problems on the tables and figures>>

    Yes, RTF isn't Word's standard file format, so that could cause problems.

    <<"more efficient for you to be doing the document manipulation using the Office Open XML file format, rather than working over the interop"

    --will i be restarting my project when i use the office open xml file format? can i still use my c# windows form application?>>

    That's a good question... Yes, you could continue to use your Windows Form, but most of code you've written for putting things into Word you'd no longer need. The good news would be that you could probably create a Transform for RTF->Word document that would probably take care of the issues you've been having. It would give you full control over the result without needing to work out how Word is handling the conversion "under the covers".

    <<i want to keep track with the tables in the document, i'm not much familiar with bookmarks and content controls >>

    OK, in that case, let's take a look at bookmarks, first in a Word document as an end-user. Open any document with a table, or start a new one type a bit of text and insert a table. Now select all the rows of the table, then: Insert/Links/Bookmark. Type in a name and click "Add".

    Click the Show/Hide ¶ button in Home/Paragraph.

    Click the big, round "Office button" (top-left), choose "Word Options" and go to Advanced. In the "Show Document content" section, activate "Show bookmarks". OK.

    In the document you should now see square brackets [ ] starting in the first table cell and ending just outside the last table cell. This is the content of the bookmark. Click anywhere in the document.

    Now press Alt+F11 (opens the Word VBA IDE), press Ctrl+G (opens the Immediate Window) and type the following, finishing by pressing Enter:

       ActiveDocument.Bookmarks("test").Range.Select

    Switch back to the document (Alt+Tab) and look at the result: the table should be selected.

    Go back to the Immediate window and type the following, finishing by pressing Enter:

       ?ActiveDocument.Bookmarks("test").Range.Tables.Count

    You should get the result 1 (one). So you can see how you get the table from the bookmark.

    (I show you how to do this in Word's own IDE as it's much quicker and simpler than creating an entire WinForms application!)

    In C#, things are a bit more complex because the Bookmarks collection can take an int or a string to specify the index. So in C#:

    Word.Table tbl = doc.Bookmarks.get_Item("test").Range.Tables[1];


    Cindy Meister, VSTO/Word MVP
    Thursday, January 13, 2011 5:40 PM
    Moderator
  • Thank you so much ma'am but I don't know what should be my next question to you to solve my situation

    Will i still be using the richtextbox in my c# application for the text input of the user?

     

    <<create a Transform for RTF->Word document that would probably take care of the issues you've been having. It would give you full control over the result without needing to work out how Word is handling the conversion "under the covers".>>

    -how will i do this? is it with the use of the open xml file format ma'am?

    Can you give me sample codes for putting the things into Word? =)

     

    <<Word.Table tbl = doc.Bookmarks.get_Item("test").Range.Tables[1];>>

    -this code is for? I'm sorry ma'am, how about the insertion of tables using my c# application? how will it be done?

     

    In addition to the description of what I'm doing ma'am, I'm using MySQL database for storing the user input from the richtextbox in my c# application to the generated MSWord document. I'm using the database so that the user can edit his work in my c# application and then generate the word document whenever he wants to print. I'm also using a template to automatically format the text added in the Word document.

     

    Thank you so much ma'am for your patience, You are giving me the hope to finish my project

    Thursday, January 13, 2011 7:15 PM
  • Hi hmFG

    <<Will i still be using the richtextbox in my c# application for the text input of the user?>>

    If we're talking about the suggested alternate approach of using OpenXML, the answer is YES.

    <<-how will i do this? is it with the use of the open xml file format ma'am?

    Can you give me sample codes for putting the things into Word? =)>>

    For more assistance on the OpenXML approach you need to go to OpenXMLDeveloper.org and the Open XML SDK forum. I cannot help you with the details of how this works. I only know that it's possible. (I'm an interop object model specialist - can't be good at everything :-))

    <<<<Word.Table tbl = doc.Bookmarks.get_Item("test").Range.Tables[1];>>

    -this code is for? I'm sorry ma'am, how about the insertion of tables using my c# application? how will it be done?>>

    How tables can be inserted in Word using the Interop was settled in the other discussion, with the link to that MSDN article. Your follow-up question was how to update an existing table. I asked you if you knew how to identify things in a Word document and you answered you did not. In my last reply, I then described how to work with bookmarks in the UI.

    This code snippet shows how to get the table object within a bookmark using C# code. But you have to go through all the steps I outline in order to understand what's going on. I don't write out all that text for no reason...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bessie Zhao Wednesday, January 19, 2011 3:56 AM
    Sunday, January 16, 2011 8:33 AM
    Moderator