none
How to insert data into a specific cell in a table? RRS feed

  • 問題

  • I always get an error message. "Object reference not set to an instance of an object"
    Can anyone how to solve this?
    Here is my code.


    Code Snippet

    private void CreateNewWordFile(string sourceFileName,string destinationFileName,string stratBookmarkName,string endBookmarkName)
            {
                // Declaring the object variables we will need later
                object varFileName = fileName;
                object varFalseValue = false;
                object varTrueValue = true;
                object varMissing = Type.Missing;
                // Create a reference to MS Word application
                Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();

                // Creates a reference to a word document
                Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
                // Activate the document
                varDoc.Activate();

                string test = varDoc.Bookmarks.Count.ToString();

                //BOOK MARK FOR START OF SELECTION
                object oBookmarkStart = "bookmark1";
                object oRngoBookMarkStart = varDoc.Bookmarks.get_Item(ref oBookmarkStart).Range.Start;

                //BOOK MARK FOR END OF SELECTION
                object oBookmarkEnd = "bookmark2";
                object oRngoBookMarkEnd = varDoc.Bookmarks.get_Item(ref oBookmarkEnd).Range.Start;

                //SETTING THE RANGE ON THE BOOKMARK BETWEEN TWO BOOKMARKS
                Word.Range rngBKMarkSelection = varDoc.Range(ref oRngoBookMarkStart, ref oRngoBookMarkEnd);

                //SELECTING THE TEXT
                rngBKMarkSelection.Select();
                rngBKMarkSelection.Copy();
               
                CreateNewDocument(destinationFileName);
                InsertTableIntoNewDocument(destinationFileName);
                InsertDataIntoTable(destinationFileName, rngBKMarkSelection);

                //CLOSING THE FILE
                varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
                //QUITTING THE APPLICATION
                varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
            }
            private void InsertDataIntoTable(string destinationFileName, Word.Range rngBKMarkSelection)
            {
                try
                {
                    // Declaring the object variables we will need later
                    object varFileName = destinationFileName;
                    object varFalseValue = false;
                    object varTrueValue = true;
                    object varMissing = Type.Missing;
                    // Create a reference to MS Word application
                    Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();

                    // Creates a reference to a word document
                    Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing,
                                                                                           ref varMissing, ref varMissing, ref varMissing, ref varMissing,
                                                                                           ref varMissing, ref varMissing, ref varMissing, ref varMissing,
                                                                                           ref varMissing, ref varMissing, ref varMissing, ref varMissing);
                    // Activate the document
                    varDoc.Activate();

                    //error code here
                    object oUpperHeadingLevel = "1";
                    object oLowerHeadingLevel = "1";
                    object oTOCTableID = varDoc.Tables[1].ID.ToString();
                    varDoc.TablesOfContents.Add(rngBKMarkSelection, ref varTrueValue, ref oUpperHeadingLevel,
                                                    ref oLowerHeadingLevel, ref varMissing, ref oTOCTableID, ref varTrueValue,
                                                    ref varTrueValue, ref varMissing, ref varTrueValue, ref varTrueValue, ref varTrueValue);
                    //error code here

                    //THE LOCATION WHERE THE FILE NEEDS TO BE SAVED
                    object oSaveAsFile = destinationFileName;
                    varDoc.SaveAs(
                        ref oSaveAsFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
                        ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
                        ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);

                    //CLOSING THE FILE
                    varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
                    //QUITTING THE APPLICATION
                    varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
                }
                catch (Exception varE)
                {
                    MessageBox.Show("Error:\n" + varE.Message, "Error message");
                }
            }

     

     

    2007年7月24日 上午 02:43

解答

  • I just find a solution.
      Here is the source code.
      Hope it helps. 
     

    Code Snippet

    private void SelectWantedData(string fileName, string startBookmark, string endBookmark)
            {
                try
                {
                    // Declaring the object variables we will need later
                    object varFileName = fileName;
                    object varFalseValue = false;
                    object varTrueValue = true;
                    object varMissing = Type.Missing;
                    // Create a reference to MS Word application
                    Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();

                    // Creates a reference to a word document
                    Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(
                        ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing,
                        ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
                        ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
                        ref varMissing);
                    // Activate the document
                    varDoc.Activate();

                    string test = varDoc.Bookmarks.Count.ToString();

                    //BOOK MARK FOR START OF SELECTION
                    object oBookmarkStart = startBookmark;
                    object oRngoBookMarkStart = varDoc.Bookmarks.get_Item(ref oBookmarkStart).Range.End;

                    //BOOK MARK FOR END OF SELECTION
                    object oBookmarkEnd = endBookmark;
                    object oRngoBookMarkEnd = varDoc.Bookmarks.get_Item(ref oBookmarkEnd).Range.Start;

                    //SETTING THE RANGE ON THE BOOKMARK BETWEEN TWO BOOKMARKS
                    Word.Range rngBKMarkSelection = varDoc.Range(ref oRngoBookMarkStart, ref oRngoBookMarkEnd);

                    //SELECTING THE TEXT
                    rngBKMarkSelection.Select();
                    rngBKMarkSelection.Copy();

                    //CLOSING THE FILE
                    varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
                    //QUITTING THE APPLICATION
                    varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
                }
                catch (Exception varE)
                {
                    MessageBox.Show("Error:\n" + varE.Message, "Error message");
                }
            }     
            private void InsertDataIntoTable(string destinationFileName)
            {
                try
                {
                    // Declaring the object variables we will need later
                    object varFileName = destinationFileName;
                    object varFalseValue = false;
                    object varTrueValue = true;
                    object varMissing = Type.Missing;
                    // Create a reference to MS Word application
                    Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();

                    // Creates a reference to a word document
                    Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(
                        ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing,
                        ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
                        ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
                        ref varMissing);
                    // Activate the document
                    varDoc.Activate();

                    //paste data                
                    Word.Cell cell = varDoc.Tables[1].Cell(1, 1);
                    cell.Range.Paste(); 

                    //THE LOCATION WHERE THE FILE NEEDS TO BE SAVED
                    object oSaveAsFile = destinationFileName;
                    varDoc.SaveAs(
                        ref oSaveAsFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
                        ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
                        ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);

                    //CLOSING THE FILE
                    varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
                    //QUITTING THE APPLICATION
                    varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
                }
                catch (Exception varE)
                {
                    MessageBox.Show("Error:\n" + varE.Message, "Error message");
                }
            }

     

    2007年7月24日 上午 07:37