By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,008 Members | 2,962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,008 IT Pros & Developers. It's quick & easy.

How to insert data into a specific cell in a table?

P: 50
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.


Expand|Select|Wrap|Line Numbers
  1. private void CreateNewWordFile(string sourceFileName,string destinationFileName,string stratBookmarkName,string endBookmarkName)
  2.         {
  3.             // Declaring the object variables we will need later
  4.             object varFileName = fileName;
  5.             object varFalseValue = false;
  6.             object varTrueValue = true;
  7.             object varMissing = Type.Missing;
  8.             // Create a reference to MS Word application
  9.             Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
  10.  
  11.             // Creates a reference to a word document
  12.             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);
  13.             // Activate the document
  14.             varDoc.Activate();
  15.  
  16.             string test = varDoc.Bookmarks.Count.ToString();
  17.  
  18.             //BOOK MARK FOR START OF SELECTION
  19.             object oBookmarkStart = "bookmark1";
  20.             object oRngoBookMarkStart = varDoc.Bookmarks.get_Item(ref oBookmarkStart).Range.Start;
  21.  
  22.             //BOOK MARK FOR END OF SELECTION
  23.             object oBookmarkEnd = "bookmark2";
  24.             object oRngoBookMarkEnd = varDoc.Bookmarks.get_Item(ref oBookmarkEnd).Range.Start;
  25.  
  26.             //SETTING THE RANGE ON THE BOOKMARK BETWEEN TWO BOOKMARKS
  27.             Word.Range rngBKMarkSelection = varDoc.Range(ref oRngoBookMarkStart, ref oRngoBookMarkEnd);
  28.  
  29.             //SELECTING THE TEXT
  30.             rngBKMarkSelection.Select();
  31.             rngBKMarkSelection.Copy();
  32.  
  33.             CreateNewDocument(destinationFileName);
  34.             InsertTableIntoNewDocument(destinationFileName);
  35.             InsertDataIntoTable(destinationFileName, rngBKMarkSelection);
  36.  
  37.             //CLOSING THE FILE
  38.             varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
  39.             //QUITTING THE APPLICATION
  40.             varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
  41.         }
  42.         private void InsertDataIntoTable(string destinationFileName, Word.Range rngBKMarkSelection)
  43.         {
  44.             try
  45.             {
  46.                 // Declaring the object variables we will need later
  47.                 object varFileName = destinationFileName;
  48.                 object varFalseValue = false;
  49.                 object varTrueValue = true;
  50.                 object varMissing = Type.Missing;
  51.                 // Create a reference to MS Word application
  52.                 Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
  53.  
  54.                 // Creates a reference to a word document
  55.                 Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing,
  56.                                                                                        ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  57.                                                                                        ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  58.                                                                                        ref varMissing, ref varMissing, ref varMissing, ref varMissing);
  59.                 // Activate the document
  60.                 varDoc.Activate();
  61.  
  62.                 //error code here
  63.                 object oUpperHeadingLevel = "1";
  64.                 object oLowerHeadingLevel = "1";
  65.                 object oTOCTableID = varDoc.Tables[1].ID.ToString();
  66.                 varDoc.TablesOfContents.Add(rngBKMarkSelection, ref varTrueValue, ref oUpperHeadingLevel,
  67.                                                 ref oLowerHeadingLevel, ref varMissing, ref oTOCTableID, ref varTrueValue,
  68.                                                 ref varTrueValue, ref varMissing, ref varTrueValue, ref varTrueValue, ref varTrueValue);
  69.                 //error code here
  70.  
  71.                 //THE LOCATION WHERE THE FILE NEEDS TO BE SAVED
  72.                 object oSaveAsFile = destinationFileName;
  73.                 varDoc.SaveAs(
  74.                     ref oSaveAsFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  75.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  76.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
  77.  
  78.                 //CLOSING THE FILE
  79.                 varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
  80.                 //QUITTING THE APPLICATION
  81.                 varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
  82.             }
  83.             catch (Exception varE)
  84.             {
  85.                 MessageBox.Show("Error:\n" + varE.Message, "Error message");
  86.             }
  87.         }
Jul 24 '07 #1
Share this Question
Share on Google+
6 Replies


kenobewan
Expert 2.5K+
P: 4,871
What is the line number?
Jul 24 '07 #2

P: 50
What is the line number?
62 to 69
Thank you very much.
Jul 24 '07 #3

radcaesar
Expert 100+
P: 759
Debug line by line and find the exact line number where the exception was thrown using F11.
Jul 24 '07 #4

P: 50
Debug line by line and find the exact line number where the exception was thrown using F11.
Thanks for your suggestion.
I just solve the problem.
below is my source code.

Expand|Select|Wrap|Line Numbers
  1. private void SelectWantedData(string fileName, string startBookmark, string endBookmark)
  2.         {
  3.             try
  4.             {
  5.                 // Declaring the object variables we will need later
  6.                 object varFileName = fileName;
  7.                 object varFalseValue = false;
  8.                 object varTrueValue = true;
  9.                 object varMissing = Type.Missing;
  10.                 // Create a reference to MS Word application
  11.                 Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
  12.  
  13.                 // Creates a reference to a word document
  14.                 Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(
  15.                     ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing,
  16.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  17.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  18.                     ref varMissing);
  19.                 // Activate the document
  20.                 varDoc.Activate();
  21.  
  22.                 string test = varDoc.Bookmarks.Count.ToString();
  23.  
  24.                 //BOOK MARK FOR START OF SELECTION
  25.                 object oBookmarkStart = startBookmark;
  26.                 object oRngoBookMarkStart = varDoc.Bookmarks.get_Item(ref oBookmarkStart).Range.End;
  27.  
  28.                 //BOOK MARK FOR END OF SELECTION
  29.                 object oBookmarkEnd = endBookmark;
  30.                 object oRngoBookMarkEnd = varDoc.Bookmarks.get_Item(ref oBookmarkEnd).Range.Start;
  31.  
  32.                 //SETTING THE RANGE ON THE BOOKMARK BETWEEN TWO BOOKMARKS
  33.                 Word.Range rngBKMarkSelection = varDoc.Range(ref oRngoBookMarkStart, ref oRngoBookMarkEnd);
  34.  
  35.                 //SELECTING THE TEXT
  36.                 rngBKMarkSelection.Select();
  37.                 rngBKMarkSelection.Copy();
  38.  
  39.                 //CLOSING THE FILE
  40.                 varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
  41.                 //QUITTING THE APPLICATION
  42.                 varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
  43.             }
  44.             catch (Exception varE)
  45.             {
  46.                 MessageBox.Show("Error:\n" + varE.Message, "Error message");
  47.             }
  48.         }     
  49.         private void InsertDataIntoTable(string destinationFileName)
  50.         {
  51.             try
  52.             {
  53.                 // Declaring the object variables we will need later
  54.                 object varFileName = destinationFileName;
  55.                 object varFalseValue = false;
  56.                 object varTrueValue = true;
  57.                 object varMissing = Type.Missing;
  58.                 // Create a reference to MS Word application
  59.                 Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
  60.  
  61.                 // Creates a reference to a word document
  62.                 Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(
  63.                     ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing,
  64.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  65.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  66.                     ref varMissing);
  67.                 // Activate the document
  68.                 varDoc.Activate();
  69.  
  70.                 //paste data                
  71.                 Word.Cell cell = varDoc.Tables[1].Cell(1, 1);
  72.                 cell.Range.Paste(); 
  73.  
  74.                 //THE LOCATION WHERE THE FILE NEEDS TO BE SAVED
  75.                 object oSaveAsFile = destinationFileName;
  76.                 varDoc.SaveAs(
  77.                     ref oSaveAsFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  78.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
  79.                     ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
  80.  
  81.                 //CLOSING THE FILE
  82.                 varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
  83.                 //QUITTING THE APPLICATION
  84.                 varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
  85.             }
  86.             catch (Exception varE)
  87.             {
  88.                 MessageBox.Show("Error:\n" + varE.Message, "Error message");
  89.             }
  90.         }
Jul 25 '07 #5

P: 78
You really should consider NOT using objects for every single variable you use.
Jul 25 '07 #6

P: 50
You really should consider NOT using objects for every single variable you use.
Do you have any idea how to rewrite the codes?
The discussion is open to suggestions.
Many thanks for your replying.
Jul 26 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.