none
Word document 2007 in VS2010 with tables RRS feed

  • Question

  • Hi all, I'm trying to find examples for word 2007 with tables, because I'm reading information in the msdn web and I'm having problems. Please, can you refer me to an example that you can create a word table and in each box you can write the data of a TextBox of a Windows Form during the time?

    And it would be possible to write when the Word document is opened? I don't think so.

    Thanks very much!

    • Moved by Aspen VJ Monday, March 14, 2011 7:32 AM (From:Visual C# General)
    Saturday, March 12, 2011 11:49 AM

All replies

  • The problem is that I don't know how many rows I need because the time I get data is different.

    I need 4 columns and a lot of rows, is there a way I can write 4 data. Write a lot of tables (1row,4columns) will be a viable solution?

    Another question, how can I draw borders? I write oTable.Columns[2].Borders.Shadow=true; it draws well but then an error appears.

    Thanks!

    Saturday, March 12, 2011 12:34 PM
  • Hi Marcos,

    This forum is dedicated to Visual C#. I'm moving this thread from Base "C# general" to "word for developers" forum, since this issue is related to office word 2007. There are more office word 2007 experts in that forum.

    Thank you for understanding and supporting.

     

    Best Regards,

    Vin Jin

     


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 14, 2011 7:31 AM
  • Hi Marcos,

    Thank you for posting and we are glad to help with you.

    After reading your post, I wrote the following code snippet:

        private void button1_Click(object sender, EventArgs e)
        {
          Word.Application wordApp = null;
          Word.Document wordDoc = null;
          try
          {
            wordApp = new Word.Application();
            wordApp.Visible = true;
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "TableCells.docx";
            wordDoc = wordApp.Documents.Open(filePath,
       Type.Missing, Type.Missing, Type.Missing,
       Type.Missing, Type.Missing, Type.Missing,
       Type.Missing, Type.Missing, Type.Missing,
       Type.Missing, Type.Missing, Type.Missing,
       Type.Missing, Type.Missing, Type.Missing);
            Word.Range rng = wordDoc.Range(0, 0);
            // move to the end
            rng.SetRange(rng.End, rng.End);
            // add table to word document
            wordDoc.Tables.Add(rng, 1, 4, Type.Missing, Type.Missing);
    
            Word.Table wordTable = wordDoc.Tables[1] as Word.Table;
            wordTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
            wordTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
            string value1 = this.textBox1.Text;
            string value2 = this.textBox2.Text;
            wordTable.Cell(1, 1).Range.Text = value1;
            wordTable.Cell(1, 2).Range.Text = value2;
          }
          catch (Exception ex)
          {
            MessageBox.Show(ex.ToString());
          }
        }
    
    

    I think you can click the button on the from and insert table to the word document and then fill the value from the textbox to the table cell.

     In order to set the border of the table, just use the code:

    wordTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
    wordTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;

    I hope this can help you and feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Bruce Song Wednesday, March 16, 2011 9:57 AM
    Wednesday, March 16, 2011 9:56 AM
  • Hi, thanks very much for answering!! Do you think the same code could would serve for a tick event of a timer? The generation of the document must be automatic(one table(1,4) each minute ), so I can't press a button. Sorry for my ignorance but with:

       try
    {
    wordApp = new Word.Application();
    Only create a word document the first time?

    Thanks again, and thanks for the way to display the borders.

    Regards.
    Saturday, March 19, 2011 11:03 AM