none
Updating the Range a word document. RRS feed

  • Question

  • Hi ,

        I need to populate the data for the tables and pragraphs in a predefined word template. for this i am using Range to get perticular bookmarked portion from the document. please check the below code.

     object oEndOfDoc1 = "DeleteTable";  Range wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc1).Range;

     Microsoft.Office.Interop.Word.Table newTable;

      newTable = wrdRng.Tables[1];

    i am assigning the newTable to GridView and editing the table content in Gridview.

    The modified Gridview content i need to save to word document.

    i used below code to save the changes to word document.

     wrdRng.Document.Save();

    But it is throwing a error :

    The Save method or property is not available because the current

    document is not available in full screen reading.

    Can you please let me know how to save part(Range) of document.

    
    

     


    sandyjay

    Friday, January 4, 2013 9:24 AM

Answers

  • Hi sandjay

    What is a "POC"?

    The Word API has no term "datatable", please provide more detailed information about "I am looping through each row of word datatable and assigning to a datatable and assigning the datatable to Gridview datasourse."

    Also, please provide more information about how you attempt to "update the document".

    Should your project interact with the user within the Word application interface in any way?


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, January 4, 2013 11:33 AM
    Moderator
  • Hi sandyjay

    <<it is not possible to assign DataTable to word table>>

    Yes - but I wanted to be sure exactly where you were in the process before saying something like that :-)

    Strictly speaking, you'd have to do the reverse of what you do to read the data from the Word table: "walk" the cells, one-by-one. This process is, however, comparatively slow.

    There are some other approaches outlined in this article:

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

    But if you're targeting version 2007 file formats (*.docx, etc and NOT *.doc) then most efficient will probably be to use the Office Open file format and possibly the Open XML SDK tool. But this won't work if the document must be open at the same time in the Word application - it can only work on closed documents.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, January 4, 2013 4:02 PM
    Moderator

All replies

  • Hi sandjay

    I'm not famliar with how Word table content is assigned to a GridView. Could you please provide more details on that point?

    Also, which versions of Word and VSTO are involved?

    Is your code in a document-level customization or an application add-in?


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, January 4, 2013 10:45 AM
    Moderator
  • Hi Cindy,

       Thanks for your instant reply.

    I am looping through each row of word datatable and assigning to a datatable and assigning the datatable to Gridview datasourse.

    And again assigning the gridview data sourse to word datatable as same as above method.

    But i could not able to update the document .

    Actually i am doing a POC for a word automation project. so i am exploring all the available options for automating the word template.

    please help me how can i update the document.

    i am using word 2007 and VS 2010.


    sandyjay

    Friday, January 4, 2013 11:26 AM
  • Hi sandjay

    What is a "POC"?

    The Word API has no term "datatable", please provide more detailed information about "I am looping through each row of word datatable and assigning to a datatable and assigning the datatable to Gridview datasourse."

    Also, please provide more information about how you attempt to "update the document".

    Should your project interact with the user within the Word application interface in any way?


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, January 4, 2013 11:33 AM
    Moderator
  • Hi Cindy,

       POC -- Proof Of Concept.

    i found myself that my above apporch is not correct to save the tables to word document. because it is not possible to assign DataTable to word table. But any ways i am giving my trail code for you information.

    please check below piece of code which loops through table which is fetched from word document and assigns the data to DataTable and finally to Gridview.

    newTable -- Table fetched from word document.

    dgvFetch- DataGridView

     System.Data.DataTable dt = new System.Data.DataTable();            foreach (Microsoft.Office.Interop.Word.Cell c in r.Cells)             {                 dt.Columns.Add(c.Range.Text);             }             foreach (Microsoft.Office.Interop.Word.Row row in newTable.Rows)             {                 System.Data.DataRow dr = dt.NewRow();                 int i = 0;                 foreach (Cell cell in row.Cells)                 {                     if (!string.IsNullOrEmpty(cell.Range.Text))                     {                         dr[i] = cell.Range.Text;                     }                 }                 dt.Rows.Add(dr);                 i++;             }

     dgvFetch.DataSource = dt;

    I am trying another way .i will let you know if i am success or i will

    approch you if i have any doubts :-)

    Thanks,

    Sandy.


    sandyjay

    Friday, January 4, 2013 12:00 PM
  • Hi sandyjay

    <<it is not possible to assign DataTable to word table>>

    Yes - but I wanted to be sure exactly where you were in the process before saying something like that :-)

    Strictly speaking, you'd have to do the reverse of what you do to read the data from the Word table: "walk" the cells, one-by-one. This process is, however, comparatively slow.

    There are some other approaches outlined in this article:

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

    But if you're targeting version 2007 file formats (*.docx, etc and NOT *.doc) then most efficient will probably be to use the Office Open file format and possibly the Open XML SDK tool. But this won't work if the document must be open at the same time in the Word application - it can only work on closed documents.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, January 4, 2013 4:02 PM
    Moderator