none
Microsoft.Office.Interop.Word.Range RRS feed

  • Question

  • Hi community;

    i work for the fisrt time with the type " Microsoft.Office.Interop.Word.Range ", i dont really know how to manipulate it correctly,when i run my application it works but it gives an exception wich says that the Object reference not set to an instance of an object for this line:

     public Microsoft.Office.Interop.Word.Table CreateTable(int lignes, int colonnes, Range rng)
            {
                
                rng.InsertParagraphAfter();
    
        
    
    here is when i call my function :
     CreateTable(30,2,null);
    


    What do you think the problem is? does the error source is the value" NULL" for the range?

    Thanks in advance for replying my question!:)

    • Moved by Leo Liu - MSFT Monday, September 12, 2011 6:57 AM Moved for better support. (From:Visual C# Language)
    Friday, September 9, 2011 1:23 AM

Answers

  • Hi Leoppina,

    Thanks for your post.

    You want to add a table into your document? Then you need a range argument to specify where to insert your table, at the beginning of the document, ending of the document, or after a specified paragraph.

    1. You should do something like this:

     

            public Word.Table CreateTable(int rows, int columns, Word.Range rng)
            {
                Word.Table tb = null;
                tb = rng.Tables.Add(rng, 
                    rows, 
                    columns, 
                    Word.WdDefaultTableBehavior.wdWord9TableBehavior, 
                    Word.WdAutoFitBehavior.wdAutoFitFixed);
                return tb;
            }
    

     

    For more information about Tables.Add method, please refer to:

    http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.tables.add.aspx

    2. Call the method to insert a table at the end of the document:

     

                string fileName = @"C:\TestWord.docx";
                Word.Application oWord = new Word.Application();
                Word.Document doc = oWord.Documents.Open(fileName);
                Word.Range rng = doc.Content;
                //specify the rng
                rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
    
                Word.Table tb = CreateTable(4, 3, rng);
    

     

    If you have any further concern about this, please feel free to let me know.

    Good day,


    Best Regards, Calvin Gao [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, September 12, 2011 7:26 AM
    Moderator

All replies

  • leoppina,

      you can post this question in msdn office forum to get better results.


    Mark Answered, if it solves your question
    Rohit Arora
    Friday, September 9, 2011 8:54 AM
  • Hi Leoppina,

    Thanks for your post.

    You want to add a table into your document? Then you need a range argument to specify where to insert your table, at the beginning of the document, ending of the document, or after a specified paragraph.

    1. You should do something like this:

     

            public Word.Table CreateTable(int rows, int columns, Word.Range rng)
            {
                Word.Table tb = null;
                tb = rng.Tables.Add(rng, 
                    rows, 
                    columns, 
                    Word.WdDefaultTableBehavior.wdWord9TableBehavior, 
                    Word.WdAutoFitBehavior.wdAutoFitFixed);
                return tb;
            }
    

     

    For more information about Tables.Add method, please refer to:

    http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.tables.add.aspx

    2. Call the method to insert a table at the end of the document:

     

                string fileName = @"C:\TestWord.docx";
                Word.Application oWord = new Word.Application();
                Word.Document doc = oWord.Documents.Open(fileName);
                Word.Range rng = doc.Content;
                //specify the rng
                rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
    
                Word.Table tb = CreateTable(4, 3, rng);
    

     

    If you have any further concern about this, please feel free to let me know.

    Good day,


    Best Regards, Calvin Gao [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, September 12, 2011 7:26 AM
    Moderator