none
I cant seem to be able to create a table in word document RRS feed

  • Question

  • Hello everyone,

    I am trying to create a table from codebehind for in my word document. I have the following code, but I am getting some errors on it.

    object start = 0;
    object end = 0;
    Word.Range tableLocation = this.Range(ref start, ref end);
    this.Tables.Add(tableLocation, 3, 4, ref missing, ref missing);

    He doesnt recognize this.Range or this.Tables.

    Any tips would be great!

    Greetings,

    SpaceLama

    • Moved by Bob Shen Friday, May 4, 2012 6:37 AM (From:Visual C# General)
    Wednesday, May 2, 2012 6:16 PM

Answers

  • Hi SpaceLama,

    this.Range(ref start, ref end) can be used in a "Word document" project to reference a range.

    In a word automation application as you have mentioned in your last post, what you should do is as following:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace WordAuto
    {
        class Program
        {      
            static void Main()
            {
                Word.Application oWord = new Word.Application();
                oWord.Visible = true;
                Word.Document oDoc = oWord.Documents.Add();
                object oStart=0;
                object oEnd=0;
                object missing = System.Reflection.Missing.Value;
                Word.Range oRange = oDoc.Range(ref oStart, ref oEnd);
                int rowCount=3, colCount=5;
    
                Word.Table oTable = oDoc.Tables.Add(oRange, rowCount, colCount, ref missing, ref missing);
    
                int r, c;
                string strText;
                for (r = 1; r <= rowCount; r++)
                    for (c = 1; c <= colCount; c++)
                    {
                        strText = "r" + r + "c" + c;
                        oTable.Cell(r, c).Range.Text = strText;
                    }
                oTable.Rows[1].Range.Font.Bold = 1;
                oTable.Rows[1].Range.Font.Italic = 1;
               
            }
        }
    }

    You can check this KB for more information about Word Automation. 

    Also, this link provides details about Word Object Model which you may interest.

    Thanks.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 7, 2012 5:04 AM
    Moderator

All replies

  • The word this means this class/object.  So this.Range means within the object there is a global variable named RANGE.


    JP Cowboy Coders Unite!

    Thursday, May 3, 2012 2:48 AM
  • So i need to define my own global range variabel? Global... string variable? Or how is it being defined?

    Thursday, May 3, 2012 5:51 AM
  • Hello Mitja,

    Thanks for your reply. I used those sites to test my code. So when I copy there coding and apply the using then it still doesnt work.

    This is the using of the range and I have applied this one...

    Namespace:  Microsoft.Office.Interop.Word
    Assembly:  Microsoft.Office.Interop.Word (in Microsoft.Office.Interop.Word.dll)

    I see this dll within COM and .NET references... I tested both, but they dont seem to work.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Word = Microsoft.Office.Interop.Word;
    using System.Reflection;
    object start = 0, end = 0;
                Word.Range rng = this.Range(ref start, ref end);

    Error 1 'AddressBook.Helper.WriteInWord' does not contain a definition for 'Range' and no extension method 'Range' accepting a first argument of type 'AddressBook.Helper.WriteInWord' could be found (are you missing a using directive or an assembly reference?) 

    Greetings,

    Spacelama

    Thursday, May 3, 2012 7:44 AM
  • Are you trying to create a table in MS Word from DataTable? Or some other collection?

    Mitja

    Thursday, May 3, 2012 8:24 AM
  • Hi SpaceLama,

    this.Range(ref start, ref end) can be used in a "Word document" project to reference a range.

    In a word automation application as you have mentioned in your last post, what you should do is as following:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace WordAuto
    {
        class Program
        {      
            static void Main()
            {
                Word.Application oWord = new Word.Application();
                oWord.Visible = true;
                Word.Document oDoc = oWord.Documents.Add();
                object oStart=0;
                object oEnd=0;
                object missing = System.Reflection.Missing.Value;
                Word.Range oRange = oDoc.Range(ref oStart, ref oEnd);
                int rowCount=3, colCount=5;
    
                Word.Table oTable = oDoc.Tables.Add(oRange, rowCount, colCount, ref missing, ref missing);
    
                int r, c;
                string strText;
                for (r = 1; r <= rowCount; r++)
                    for (c = 1; c <= colCount; c++)
                    {
                        strText = "r" + r + "c" + c;
                        oTable.Cell(r, c).Range.Text = strText;
                    }
                oTable.Rows[1].Range.Font.Bold = 1;
                oTable.Rows[1].Range.Font.Italic = 1;
               
            }
        }
    }

    You can check this KB for more information about Word Automation. 

    Also, this link provides details about Word Object Model which you may interest.

    Thanks.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 7, 2012 5:04 AM
    Moderator